2017-07-15 11 views
0

親のテキストと画像の色合いをある色に設定する方法があります。今、親と前景の色合い(私が設定している色合い)が対照的に近い場合、テキストは読み込みできなくなります。2色のコントラストの差を見つける

これら2つの色の違いを確認して、どれが読みやすくなるようにするにはどうすればよいですか(明るくなるか暗くする)か?

public static void invokeContrastSafety(ViewGroup parent, int tint, boolean shouldPreserveForeground) { 
    Drawable background = parent.getBackground(); 
    if (background instanceof ColorDrawable) { 
     if (isColorDark(((ColorDrawable) background).getColor())) { 
      // Parent background is dark 

      if (isColorDark(tint)) { 
       // Tint (foreground) color is also dark. 
       if (shouldPreserveForeground) { 
        // We can't modify tint color, changing background to make things readable. 


       } else { 
        // Altering foreground to make things readable 

       } 

       invokeInternal(parent, tint); 
      } else { 
       // Everything is readable. Just pass it on. 
       invokeInternal(parent, tint); 
      } 

     } else { 
      // Parent background is light 

      if (!isColorDark(tint)) { 
       if (shouldPreserveForeground) { 

       } else { 

       } 
      } else { 
       invokeInternal(parent, tint); 
      } 
     } 
    } 
} 

private static boolean isColorDark(int color){ 
    double darkness = 1-(0.299* Color.red(color) + 0.587*Color.green(color) + 0.114*Color.blue(color))/255; 
    return darkness >= 0.2; 
} 
+0

私の答えを参照してくださいこれはあなたを助けることを願っています。 –

+0

[RGB色の明るさを決定する式]の可能な複製(https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color) – geza

答えて

0

は、それが私の作品、この方法を試してください。ここで

は私が今まで持っているものです。この関数は、色が暗いか明るいかを返します。

public static boolean isBrightColor(int color) { 
    if (android.R.color.transparent == color) 
     return true; 

    boolean rtnValue = false; 

    int[] rgb = { Color.red(color), Color.green(color), Color.blue(color) }; 

    int brightness = (int) Math.sqrt(rgb[0] * rgb[0] * .241 + rgb[1] 
      * rgb[1] * .691 + rgb[2] * rgb[2] * .068); 

    // Color is Light 
    if (brightness >= 200) { 
     rtnValue = true; 
    } 
    return rtnValue; 
} 

必要に応じてこの機能でカスタムロジックを変更できます。

+0

「isColorDarkを追加しないと悪い) '関数を呼び出すことができます。私はすでにそれを確認する方法を知っています。 –

+0

次に問題は何ですか? –

+0

ロジックの空白を残して、指定された背景色で簡単に読み取れるようになるまで、色をより暗くまたはより明るくしました。 –

関連する問題