- public static @ColorLong
- long hsvaColor(float hue, float saturation, float value, float alpha) {
- if (alpha < 0 || alpha > 1)
- throw new IllegalArgumentException("alpha must be between 0 and 1");
-
- @ColorLong long rgb = hsvTriplet(hue, saturation, value);
-
- long a_bits = Math.round(255 * alpha);
- return (a_bits << 24) | rgb;
- }
- public static @ColorInt
- int hsvColor(float hue, float saturation, float value) {
- return 0xff000000 | hsvTriplet(hue, saturation, value);
- }
- public static @ColorInt
- int hslColor(float hueRatio, float saturation, float lightness) {
- return 0xff000000 | hslTriplet(hueRatio, saturation, lightness);
- }
- public static @ColorInt
- int hsvTriplet(float hue, float saturation, float value) {
- @ColorLong long result;
- int r, g, b;
-
- if ((hue < -0.00005) || (hue > 1.0000005) || (saturation < 0) || (saturation > 1) ||
- (value < 0) || (value > 1))
- throw new IllegalArgumentException(String.format(
- "hue, saturation, value and alpha must all be between 0 and 1. Arguments " +
- "given: " + "hue=%1.5f, sat=%1.5f, val=%1.5f", hue, saturation, value));
-
- int h = (int) (hue * 6);
- float f = hue * 6 - h;
- float p = value * (1 - saturation);
- float q = value * (1 - f * saturation);
- float t = value * (1 - (1 - f) * saturation);
-
- switch (h) {
- case 0:
- case 6:
- return tupleToColor(value, t, p);
- case 1:
- return tupleToColor(q, value, p);
- case 2:
- return tupleToColor(p, value, t);
- case 3:
- return tupleToColor(p, q, value);
- case 4:
- return tupleToColor(t, p, value);
- case 5:
- return tupleToColor(value, p, q);
- default:
- throw new RuntimeException(String.format("Unexpected value for h (%d) while " +
- "converting hsv(%1.2f, %1.2f, %1.2f) to " +
- "rgb", h, hue, saturation, value));
- }
- }
- public static @ColorInt
- int hslTriplet(float hueRatio, float saturation, float lightness) {
- @ColorLong long result;
- float h = hueRatio * 6;
- float c = (1 - abs(2f * lightness - 1)) * saturation;
- float h_mod_2 = h % 2;
- float x = c * (1 - Math.abs(h_mod_2 - 1));
- int r, g, b;
- float m = lightness - c / 2f;
-
- if (h < 1 || h == 6)
- return tupleToColor(c + m, x + m, 0 + m);
- if (h < 2)
- return tupleToColor(x + m, c + m, 0 + m);
- if (h < 3)
- return tupleToColor(0 + m, c + m, x + m);
- if (h < 4)
- return tupleToColor(0 + m, x + m, c + m);
- if (h < 5)
- return tupleToColor(x + m, 0 + m, c + m);
- if (h < 6)
- return tupleToColor(c + m, 0 + m, x + m);
-
- throw new IllegalArgumentException(String.format(
- "Unexpected value for h (%1.3f) while converting hsl(%1.3f, %1.3f, %1.3f) to rgb",
- h, hueRatio, saturation, lightness));
- }
-
- public static @ColorInt
- int tupleToColor(float r, float g, float b) {
- int r_int = Math.round(255 * r);
- int g_int = Math.round(255 * g);
- int b_int = Math.round(255 * b);
- return (r_int << 16) | (g_int << 8) | b_int;
- }