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; }
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 ).
darken. darken: function(amount = 10) -> TinyColor . Darken the color a given amount, from 0 to 100. Providing 100 will always return black.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With