What color mixing algorithm is used to darken the color?

I have these cards with two color shades. Primary color and then darker accent color:

The primary color is given to me in hexadecimal, but not in accent. Can you tell me which blend or transformation is done for the ARGB primary color to get a darker accent color?

If that matters, I am developing against Android, so I have access to the Color and ColorFilter class, so I can apply all that PorterDuff stuff is ...

enter image description here

+4
source share
6 answers

If you want a darker Color , you can:

  • Convert RGB to HSV with RGBToHSV() .
  • Decrease V (luminance value). It is hsv[2] , a float with a value from 0 to 1.
  • Convert HSV to Color with HSVToColor() .

If you want a darker Bitmap , PorterDuff.Mode.DARKEN should work. You just need to calibrate the Color :

 private Bitmap getDarkerBitmap(Bitmap src) { final int COLOR = 0xAAFFFFFF; final int WIDTH = src.getWidth(); final int HEIGHT = src.getHeight(); final Bitmap result = Bitmap.createBitmap(WIDTH, HEIGHT, src.getConfig()); final BitmapDrawable drawable = new BitmapDrawable(src); drawable.setBounds(0, 0, WIDTH, HEIGHT); drawable.setColorFilter(COLOR, PorterDuff.Mode.DARKEN); drawable.draw(new Canvas(result)); return result; } 
+4
source

Just do the following:

 int color = .... int r = Color.red(color); int b = Color.blue(color); int g = Color.green(color); int darkerColor = Color.rgb((int)(r*.9), (int)(g*.9), (int)(b*.9)); 
+2
source

Using this code, where, if the coefficient <1, the color of the result will be darker, and if the coefficient> 1, the color of the result will be lighter.

 public static int alterColor(int color, float factor) { int a = (color & (0xFF << 24)) >> 24; int r = (int) (((color & (0xFF << 16)) >> 16) * factor); int g = (int) (((color & (0xFF << 8)) >> 8) * factor); int b = (int) ((color & 0xFF) * factor); return Color.argb(a, r, g, b); } 
+2
source

I review the answers provided. They are wonderful. But here is another way to darken int colors in Android. As below:

  • Convert your RGB color to HSV using the RGBToHSV() method
  • Decrease V (luminance value) using float factor. It is located in hsv[2] , and the floating point number ranges from 0 to 1.
  • Convert HSV to color using HSVToColor() and then return

This is a snippet:

 public static int getDarkerColor(int color, float factor) { float[] hsv = new float[3]; Color.RGBToHSV(Color.red(color), Color.green(color), Color.blue(color), hsv); hsv[2] = hsv[2] * factor; return Color.HSVToColor(hsv); } 

Note that when you give factor 0.9f, you want to darken the input color at a rate of 10% ((1-0.9) * 100).

+2
source

To darken the color, you just need to remove the same amount from the red green and blue components.

Change Since you get the color from the hexadecimal value, you need to separate the components from it, reduce its value and return it back.

0xaarrggbb → a = alpha r = red g = green b = blue

 int color = 0xff445566; // Get the color component: int alpha |= a << 24; int red |= r << 16; int green |= g << 8; int blue |= b; int newalpha = alpha; int newred = red - proportional amount ; int newgreen = green - proportional amount ; int newblue = blue - proportional amount ; // Set the color component color = (color & ~(0xFF << 24)) | (newalpha << 24); color = (color & ~(0xFF << 16)) | (newred << 16); color = (color & ~(0xFF << 8)) | (newgreen << 8); color = (color & ~0xFF) | newblue; 
0
source

java.awt.Color.darker() multiplies each component 0-255 (R, G, B) by 0.7, and then returns it by int .

0
source

Source: https://habr.com/ru/post/1498978/


All Articles