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;
}
A more Android way of doing it:
You will want to use a factor less than
1.0f
to darken. try0.8f
.A simpler solution using HSV like RogueBaneling suggested:
Kotlin
Java
No complex bitwise operations or
Math
necessary. Move0.8f
to an argument if needed.There's even a simpler solution that has not been mentioned before, Android has a class called
ColorUtils
which can you help you with that here's a Kotlin snippet of an extension functionthe method
ColorUtils.blendARGB
takes your color as the first parameter and the second color you want to blend,black
in this case and finally, the last parameter is the ratio between your color and the black color you're blending.This way you can select percentage of color and thus fetch a shade darker or a shade lighter
colorlighter gives us a lighter shade of color from button's background colordarker gives us a darker shade of color from button's background
If you want more simple and not accurately, below might help you.
Convert the color to a HSV array, then reduce the brightness by 20%, then convert HSV array back to RGB with
HSVToColor
. Note: The value you are looking to change in the array will be theV
-value. (i.e.,hsv[2]
)