Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to darken a given color (int)

I have a function that takes a given color and I would like it to darken the color (reduce its brightness by 20% or so). I can't figure out how to do this given just a color (int). What is the proper approach?

public static int returnDarkerColor(int color){     int darkerColor = ....      return darkerColor; } 
like image 516
NSouth Avatar asked Oct 12 '15 02:10

NSouth


People also ask

How do you lighten a darken or hex color?

Just pass in a string like "3F6D2A" for the color ( col ) and a base10 integer ( amt ) for the amount to lighten or darken. To darken, pass in a negative number (i.e. -20 ).

How do you darken colors in flutter?

darken. darken: function(amount = 10) -> TinyColor . Darken the color a given amount, from 0 to 100. Providing 100 will always return black.

How do you make a color code darker?

Tints are lighter versions of the color that are made by mixing a color with white, whereas shades are darker versions of the color that are made by mixing a color with black. For example, pink is a tint of red, while maroon is a shade of red.


1 Answers

A more Android way of doing it:

    public static int manipulateColor(int color, float factor) {         int a = Color.alpha(color);         int r = Math.round(Color.red(color) * factor);         int g = Math.round(Color.green(color) * factor);         int b = Math.round(Color.blue(color) * factor);         return Color.argb(a,                 Math.min(r,255),                 Math.min(g,255),                 Math.min(b,255));     } 

You will want to use a factor less than 1.0f to darken. try 0.8f.

like image 156
Gary McGowan Avatar answered Oct 17 '22 08:10

Gary McGowan