3

I want to change the brightness of any given color (Note: I am not talking about screen brightness), I have looked at the Color class, it has a few methods for conversions between RGB and HSV, I'm a newbie in this area. To start with, how do I change the brightness of red, if its value is spefied in RGB (#FF0000)?

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155

4 Answers4

5

The easiest way would be to convert the color to HSL (not HSV! they are different - see http://en.wikipedia.org/wiki/HSL_and_HSV) and change the L component - increase to make it brighter, decrease to make it darker.

etarion
  • 16,935
  • 4
  • 43
  • 66
  • 4
    Code examples would be better, I was looking at theory. – Ragunath Jawahar Jan 03 '11 at 07:37
  • 1
    http://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion has a link to code examples. – etarion Jan 03 '11 at 07:48
  • It's not true. Here is the way to use HSV to change brightness, http://stackoverflow.com/a/4928826/3109401 – Kimi Chiu Sep 29 '16 at 05:53
  • HSL seems to be more appropriate indeed. If you use android support library, you can use `ColorUtils.colorToHSL()` and `ColorUtils.HSLToColor()` for conversion. – sulai Jan 09 '18 at 17:06
3

Considering that you are talking about brightness (color enhance) and not luminance (white amount), your model is the HSV (aka HSB) and not HSL.

On fast briefing, if you enhance the V channel on HSV over, lets say... some blue, you have a "more blue" color. If you enhance the L channel on HSL model you have a more "clear and washed" blue.

The android.graphics.Color class have built-in support to HSV model. Use Color.colorToHSV() and Color.HSVToColor() to edit the brightness value (or hue, or saturation, if you like).

On HSV model, H (hue) define the base color, S (saturation) control the amount of gray and V controls the brightness. So, if you enhance V and decrease S at same time, you gets more luminance, in pratice.

Renascienza
  • 1,647
  • 1
  • 13
  • 16
2

For starters, you need to remember two things -

  1. To reduce brightness, you can change red from #FF0000 to #AA0000 or #880000 - basically reduce the Red component.
  2. You can also try reducing opacity - often you'll realize that it works better than just reducing brightness.
Abhinav Manchanda
  • 6,546
  • 3
  • 39
  • 46
  • I had this idea of varying opacity, how would I reduce the brightness uniformly if I have something like #D456FB? – Ragunath Jawahar Jan 03 '11 at 07:39
  • 1
    #D456FB is equivalent to #FFD456FB, where the first two characters represent opacity. Change it to #99D456FB to get reduced opacity. If you're doing it through code and not through Xml, search for how to increase or decrease 'alpha'. – Abhinav Manchanda Jan 03 '11 at 08:23
1

You can use Color.colorToHSV to convert the color to HSV, then change the brightness of the HSV color, then use Color.HSVToColor to convert it back to a color int. For example, the following code sets the brightness to 0.5:

@ColorInt int originalColor = /*your original color*/;
float[] hsv = new float[3];    //Create an array to pass to the colorToHSV function
Color.colorToHSV(originalColor, hsv);    //Put the HSV components in the array created above
hsv[2] = 0.5f;    //Whatever brightness you want to set. 0 is black, 1 is the pure color.
@ColorInt int newColor = Color.HSVToColor(hsv);    //Convert it back to a ColorInt
Donald Duck
  • 8,409
  • 22
  • 75
  • 99