-1

In my code I have this:

public static int darkenColor(int color, float value) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    hsv[2] *= value; // value component

    return Color.HSVToColor(hsv);
}

I want to create its counterpart, lightenColor. What is the correct formula?

l33t
  • 18,692
  • 16
  • 103
  • 180

2 Answers2

0
hsv[2] = Math.pow(hsv[2], value) 

always will be in [0..1] if value >= 0. You may use that formula for both methods.

Dmitry_L
  • 1,004
  • 7
  • 8
  • And what will the other method look like? Same but with negative `value`? – l33t Jun 20 '13 at 16:17
  • if you want to incraise hsv[2] value must be in [0..1], if you need to decraise value must be in (1..infinity) - this is for one method. If you need two methods with the same range of value than you may use hsv[2]=Math.pow(hsv[2], value) for darkenColor, and hsv[2]=Math.pow(hsv[2], 1 / value) for lightenColor, where value >= 1. – Dmitry_L Jun 21 '13 at 05:37
  • The inverse `1 / value` makes sense. I cannot understand this `pow` usage though. Can you point to a source that explains why it should be used instead of multiplication? E.g. 100*0.3 != pow(100, 0.3) – l33t Jun 26 '13 at 07:35
  • I just adviced you to use `pow`, because it guarantees that your `hsv[2]` value will be always in [0..1] range. – Dmitry_L Jun 26 '13 at 20:15
0

Use this:

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

You can change color effect by changing the {hsv[2] *= 1.4f}