diff options
author | John Ahlroos <john@vaadin.com> | 2014-04-24 21:12:15 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-05-14 07:25:50 +0000 |
commit | 5a1ffa814230c3da751ab5953da0fb0ae75c4c80 (patch) | |
tree | 38c0a9826d8e69721e8db6ae16344ea02c534d67 /shared | |
parent | ac6a98648d54fc89cab5c5cecc2d9f1b9a3b01dc (diff) | |
download | vaadin-framework-5a1ffa814230c3da751ab5953da0fb0ae75c4c80.tar.gz vaadin-framework-5a1ffa814230c3da751ab5953da0fb0ae75c4c80.zip |
Allow editing colorpicker values in colorpicker in textfield #13469
This fix adds support for typing in color values according to
http://www.w3schools.com/cssref/css_colors_legal.asp into the
colorpicker popup input textfield.
Change-Id: If14ead791725c3052c05aa31e12e237e90c32348
Diffstat (limited to 'shared')
-rw-r--r-- | shared/src/com/vaadin/shared/ui/colorpicker/Color.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/shared/src/com/vaadin/shared/ui/colorpicker/Color.java b/shared/src/com/vaadin/shared/ui/colorpicker/Color.java index 7fbb0ee055..8624327993 100644 --- a/shared/src/com/vaadin/shared/ui/colorpicker/Color.java +++ b/shared/src/com/vaadin/shared/ui/colorpicker/Color.java @@ -392,4 +392,62 @@ public class Color implements Serializable { return 0xff000000 | (red << 16) | (green << 8) | (blue << 0); } + + /** + * <p> + * Converts HSL's hue, saturation and lightness into an RGB value. + * + * @param hue + * the hue of the color. The unit of the value is degrees and + * should be between 0-360. + * @param saturation + * the saturation of the color. The unit of the value is + * percentages and should be between 0-100; + * @param lightness + * the lightness of the color. The unit of the value is + * percentages and should be between 0-100; + * + * @return the RGB value of corresponding color + */ + public static int HSLtoRGB(int hue, int saturation, int lightness) { + int red = 0; + int green = 0; + int blue = 0; + + float hueRatio = hue / 360f; + float saturationRatio = saturation / 100f; + float lightnessRatio = lightness / 100f; + + if (saturationRatio == 0) { + red = green = blue = (int) (lightnessRatio * 255.0f + 0.5f); + } else { + float p = lightnessRatio < 0.5f ? lightnessRatio + * (1f + saturationRatio) : lightnessRatio + saturationRatio + - lightnessRatio * saturationRatio; + float q = 2 * lightnessRatio - p; + + red = hslComponentToRgbComponent(p, q, hueRatio + (1f / 3f)); + green = hslComponentToRgbComponent(p, q, hueRatio); + blue = hslComponentToRgbComponent(p, q, hueRatio - (1f / 3f)); + } + return 0xff000000 | (red << 16) | (green << 8) | (blue << 0); + } + + private static int hslComponentToRgbComponent(float p, float q, float ratio) { + if (ratio < 0) { + ratio += 1; + } else if (ratio > 1) { + ratio -= 1; + } + + if (6 * ratio < 1f) { + return (int) ((q + (p - q) * 6f * ratio) * 255f + 0.5f); + } else if (2f * ratio < 1f) { + return (int) (p * 255f + 0.5f); + } else if (3f * ratio < 2f) { + return (int) ((q + (p - q) * ((2f / 3f) - ratio) * 6f) * 255f + 0.5f); + } + + return (int) (q * 255f + 0.5f); + } } |