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 /server | |
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 'server')
-rw-r--r-- | server/src/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java | 76 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/server/component/colorpicker/ColorConversions.java | 57 |
2 files changed, 114 insertions, 19 deletions
diff --git a/server/src/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java b/server/src/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java index ae0f717df8..ae00b267ce 100644 --- a/server/src/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java +++ b/server/src/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java @@ -19,7 +19,6 @@ import java.lang.reflect.Method; import com.vaadin.data.Property.ValueChangeEvent; import com.vaadin.data.Property.ValueChangeListener; -import com.vaadin.data.validator.RegexpValidator; import com.vaadin.shared.ui.colorpicker.Color; import com.vaadin.ui.Component; import com.vaadin.ui.CssLayout; @@ -67,13 +66,12 @@ public class ColorPickerPreview extends CssLayout implements ColorSelector, this.color = color; field = new TextField(); - field.setReadOnly(true); field.setImmediate(true); field.setSizeFull(); field.setStyleName("v-colorpicker-preview-textfield"); field.setData(this); field.addValueChangeListener(this); - field.addValidator(new RegexpValidator("#[0-9a-fA-F]{6}", true, "")); + addComponent(field); setColor(color); @@ -85,7 +83,6 @@ public class ColorPickerPreview extends CssLayout implements ColorSelector, // Unregister listener field.removeValueChangeListener(this); - field.setReadOnly(false); String colorCSS = color.getCSS(); field.setValue(colorCSS); @@ -97,7 +94,6 @@ public class ColorPickerPreview extends CssLayout implements ColorSelector, } // Re-register listener - field.setReadOnly(true); field.addValueChangeListener(this); // Set the text color @@ -130,21 +126,63 @@ public class ColorPickerPreview extends CssLayout implements ColorSelector, @Override public void valueChange(ValueChangeEvent event) { String value = (String) event.getProperty().getValue(); - - if (!field.isValid()) { + try { + if (value != null) { + /* + * Description of supported formats see + * http://www.w3schools.com/cssref/css_colors_legal.asp + */ + if (value.length() == 7 && value.startsWith("#")) { + // CSS color format (e.g. #000000) + int red = Integer.parseInt(value.substring(1, 3), 16); + int green = Integer.parseInt(value.substring(3, 5), 16); + int blue = Integer.parseInt(value.substring(5, 7), 16); + color = new Color(red, green, blue); + + } else if (value.startsWith("rgb")) { + // RGB color format rgb/rgba(255,255,255,0.1) + String[] colors = value.substring(value.indexOf("(") + 1, + value.length() - 1).split(","); + + int red = Integer.parseInt(colors[0]); + int green = Integer.parseInt(colors[1]); + int blue = Integer.parseInt(colors[2]); + if (colors.length > 3) { + int alpha = (int) (Double.parseDouble(colors[3]) * 255d); + color = new Color(red, green, blue, alpha); + } else { + color = new Color(red, green, blue); + } + + } else if (value.startsWith("hsl")) { + // HSL color format hsl/hsla(100,50%,50%,1.0) + String[] colors = value.substring(value.indexOf("(") + 1, + value.length() - 1).split(","); + + int hue = Integer.parseInt(colors[0]); + int saturation = Integer.parseInt(colors[1] + .replace("%", "")); + int lightness = Integer + .parseInt(colors[2].replace("%", "")); + int rgb = Color.HSLtoRGB(hue, saturation, lightness); + + if (colors.length > 3) { + int alpha = (int) (Double.parseDouble(colors[3]) * 255d); + color = new Color(rgb); + color.setAlpha(alpha); + } else { + color = new Color(rgb); + } + } + + oldValue = value; + fireEvent(new ColorChangeEvent((Component) field.getData(), + color)); + } + + } catch (NumberFormatException nfe) { + // Revert value field.setValue(oldValue); - return; - } else { - oldValue = value; - } - - if (value != null && value.length() == 7) { - int red = Integer.parseInt(value.substring(1, 3), 16); - int green = Integer.parseInt(value.substring(3, 5), 16); - int blue = Integer.parseInt(value.substring(5, 7), 16); - color = new Color(red, green, blue); - - fireEvent(new ColorChangeEvent((Component) field.getData(), color)); } } diff --git a/server/tests/src/com/vaadin/tests/server/component/colorpicker/ColorConversions.java b/server/tests/src/com/vaadin/tests/server/component/colorpicker/ColorConversions.java new file mode 100644 index 0000000000..46d72a6ae6 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/colorpicker/ColorConversions.java @@ -0,0 +1,57 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component.colorpicker; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.vaadin.shared.ui.colorpicker.Color; + +public class ColorConversions { + + @Test + public void convertHSL2RGB() { + + int rgb = Color.HSLtoRGB(100, 50, 50); + Color c = new Color(rgb); + assertEquals(106, c.getRed()); + assertEquals(191, c.getGreen()); + assertEquals(64, c.getBlue()); + assertEquals("#6abf40", c.getCSS()); + + rgb = Color.HSLtoRGB(0, 50, 50); + c = new Color(rgb); + assertEquals(191, c.getRed()); + assertEquals(64, c.getGreen()); + assertEquals(64, c.getBlue()); + assertEquals("#bf4040", c.getCSS()); + + rgb = Color.HSLtoRGB(50, 0, 50); + c = new Color(rgb); + assertEquals(128, c.getRed()); + assertEquals(128, c.getGreen()); + assertEquals(128, c.getBlue()); + assertEquals("#808080", c.getCSS()); + + rgb = Color.HSLtoRGB(50, 100, 0); + c = new Color(rgb); + assertEquals(0, c.getRed(), 0); + assertEquals(0, c.getGreen(), 0); + assertEquals(0, c.getBlue(), 0); + assertEquals("#000000", c.getCSS()); + } +} |