Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What color blending algorithm is used to darken a color?

I have these cards that have two color shades on them. A main color, and then a darker accent color:

The main color is given to me in hex, but not the accent. Can you tell what kind of blend or transformation is being done to the ARGB of the main color to get the darker accent color?

If it matters, I'm developing against Android so I've got access to the Color class and ColorFilter so I can apply all that PorterDuff stuff...

enter image description here

like image 318
joepetrakovich Avatar asked Dec 20 '22 01:12

joepetrakovich


2 Answers

If you want darker Color, you can:

  1. Convert your RGB to HSV with RGBToHSV().
  2. Reduce V (lightness value). It's hsv[2], a float with value from 0 to 1.
  3. Convert HSV to Color with HSVToColor().

If you want darker Bitmap, PorterDuff.Mode.DARKEN should work. You just need to calibrate COLOR value:

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;
}
like image 132
Adam Stelmaszczyk Avatar answered Feb 05 '23 04:02

Adam Stelmaszczyk


Simply do this:

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));
like image 26
Vansuita Jr. Avatar answered Feb 05 '23 03:02

Vansuita Jr.