How to darken a given color (int)
Asked Answered
R

8

37

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;
}
Rubbish answered 12/10, 2015 at 2:26 Comment(0)
C
73

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.

Colemancolemanite answered 12/10, 2015 at 2:54 Comment(1)
Worked pretty well!Slog
N
60

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 function

inline val @receiver:ColorInt Int.darken
    @ColorInt
    get() = ColorUtils.blendARGB(this, Color.BLACK, 0.2f)

the 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.

Nightie answered 4/7, 2018 at 16:30 Comment(0)
M
27

A simpler solution using HSV like RogueBaneling suggested:

Kotlin

@ColorInt fun darkenColor(@ColorInt color: Int): Int {
    return Color.HSVToColor(FloatArray(3).apply {
        Color.colorToHSV(color, this)
        this[2] *= 0.8f
    })
}

Java

@ColorInt int darkenColor(@ColorInt int color) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    hsv[2] *= 0.8f;
    return Color.HSVToColor(hsv);
}

No complex bitwise operations or Math necessary. Move 0.8f to an argument if needed.

Metzgar answered 29/11, 2017 at 0:33 Comment(2)
what should be to lighten a color?Allegra
Change the 0.8f parameter to a value larger that 1.Metzgar
A
13

best way use the ColorUtils.blendARGB

Darker:

val newColor = ColorUtils.blendARGB(this, Color.BLACK, 0.5f)

Lighter:

val newColor = ColorUtils.blendARGB(this, Color.WHITE, 0.5f)
Amphoteric answered 9/5, 2021 at 14:38 Comment(0)
B
10

If you want more simple and not accurately, below might help you.

public static int returnDarkerColor(int color){
    float ratio = 1.0f - 0.2f;
    int a = (color >> 24) & 0xFF;
    int r = (int) (((color >> 16) & 0xFF) * ratio);
    int g = (int) (((color >> 8) & 0xFF) * ratio);
    int b = (int) ((color & 0xFF) * ratio);

    return (a << 24) | (r << 16) | (g << 8) | b;
}
Bitolj answered 12/10, 2015 at 2:45 Comment(3)
1. You're right. I just want show value 0.2f. 2, 3. It's my mistake. I just typed. Thank you for fixing.Bitolj
This works properly and is much more efficient than the accepted answerGrano
Works great. Used to set my title bar to a darkened shade of the action barHagio
U
5

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 the V-value. (i.e., hsv[2])

Umbrian answered 12/10, 2015 at 2:32 Comment(0)
B
3

Based on @Rasoul Miri answer you can create extensions to your main color class in a more kotlinic way. For example, if using the color class from the package androidx.compose.ui.graphics.Color in Android you can add some extensions to it in this way:

fun Color.lighter(factor: Float = 1f) =
  Color(ColorUtils.blendARGB(this.toArgb(), Color.White.toArgb(), factor))

fun Color.darker(factor: Float = 1f) =
  Color(ColorUtils.blendARGB(this.toArgb(), Color.Black.toArgb(), factor))

This is a very handful function that can be used by simply doing

val myNewLighterColor = MaterialTheme.colors.primary.lighter(0.5f)
val myNextDarkerColor = MaterialTheme.colors.primary.darker(0.5f)
Brougham answered 22/12, 2022 at 1:19 Comment(0)
K
0

This way you can select percentage of color and thus fetch a shade darker or a shade lighter

int  color = ((ColorDrawable)c2.getBackground()).getColor();
int  colorLighter = color - color * 40/100;
int  colorDarker = color + color * 40/100;

colorlighter gives us a lighter shade of color from button's background colordarker gives us a darker shade of color from button's background

Kaffiyeh answered 17/6, 2019 at 11:22 Comment(1)
I think you should set limit value to prevent overflowing color max value(0xFFFFFF)Agog

© 2022 - 2024 — McMap. All rights reserved.