You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColorPickerPreview.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui.components.colorpicker;
  17. import java.lang.reflect.Method;
  18. import com.vaadin.data.Property.ValueChangeEvent;
  19. import com.vaadin.data.Property.ValueChangeListener;
  20. import com.vaadin.legacy.ui.LegacyTextField;
  21. import com.vaadin.shared.ui.colorpicker.Color;
  22. import com.vaadin.ui.Component;
  23. import com.vaadin.ui.CssLayout;
  24. /**
  25. * A component that represents color selection preview within a color picker.
  26. *
  27. * @since 7.0.0
  28. */
  29. public class ColorPickerPreview extends CssLayout implements ColorSelector,
  30. ValueChangeListener {
  31. private static final String STYLE_DARK_COLOR = "v-textfield-dark";
  32. private static final String STYLE_LIGHT_COLOR = "v-textfield-light";
  33. private static final Method COLOR_CHANGE_METHOD;
  34. static {
  35. try {
  36. COLOR_CHANGE_METHOD = ColorChangeListener.class.getDeclaredMethod(
  37. "colorChanged", new Class[] { ColorChangeEvent.class });
  38. } catch (final java.lang.NoSuchMethodException e) {
  39. // This should never happen
  40. throw new java.lang.RuntimeException(
  41. "Internal error finding methods in ColorPicker");
  42. }
  43. }
  44. /** The color. */
  45. private Color color;
  46. /** The field. */
  47. private final LegacyTextField field;
  48. /** The old value. */
  49. private String oldValue;
  50. private ColorPickerPreview() {
  51. setStyleName("v-colorpicker-preview");
  52. setImmediate(true);
  53. field = new LegacyTextField();
  54. field.setImmediate(true);
  55. field.setSizeFull();
  56. field.setStyleName("v-colorpicker-preview-textfield");
  57. field.setData(this);
  58. field.addValueChangeListener(this);
  59. addComponent(field);
  60. }
  61. /**
  62. * Instantiates a new color picker preview.
  63. */
  64. public ColorPickerPreview(Color color) {
  65. this();
  66. setColor(color);
  67. }
  68. @Override
  69. public void setColor(Color color) {
  70. this.color = color;
  71. // Unregister listener
  72. field.removeValueChangeListener(this);
  73. String colorCSS = color.getCSS();
  74. field.setValue(colorCSS);
  75. if (field.isValid()) {
  76. oldValue = colorCSS;
  77. } else {
  78. field.setValue(oldValue);
  79. }
  80. // Re-register listener
  81. field.addValueChangeListener(this);
  82. // Set the text color
  83. field.removeStyleName(STYLE_DARK_COLOR);
  84. field.removeStyleName(STYLE_LIGHT_COLOR);
  85. if (this.color.getRed() + this.color.getGreen() + this.color.getBlue() < 3 * 128) {
  86. field.addStyleName(STYLE_DARK_COLOR);
  87. } else {
  88. field.addStyleName(STYLE_LIGHT_COLOR);
  89. }
  90. markAsDirty();
  91. }
  92. @Override
  93. public Color getColor() {
  94. return color;
  95. }
  96. @Override
  97. public void addColorChangeListener(ColorChangeListener listener) {
  98. addListener(ColorChangeEvent.class, listener, COLOR_CHANGE_METHOD);
  99. }
  100. @Override
  101. public void removeColorChangeListener(ColorChangeListener listener) {
  102. removeListener(ColorChangeEvent.class, listener);
  103. }
  104. @Override
  105. public void valueChange(ValueChangeEvent event) {
  106. String value = (String) event.getProperty().getValue();
  107. try {
  108. if (value != null) {
  109. /*
  110. * Description of supported formats see
  111. * http://www.w3schools.com/cssref/css_colors_legal.asp
  112. */
  113. if (value.length() == 7 && value.startsWith("#")) {
  114. // CSS color format (e.g. #000000)
  115. int red = Integer.parseInt(value.substring(1, 3), 16);
  116. int green = Integer.parseInt(value.substring(3, 5), 16);
  117. int blue = Integer.parseInt(value.substring(5, 7), 16);
  118. color = new Color(red, green, blue);
  119. } else if (value.startsWith("rgb")) {
  120. // RGB color format rgb/rgba(255,255,255,0.1)
  121. String[] colors = value.substring(value.indexOf("(") + 1,
  122. value.length() - 1).split(",");
  123. int red = Integer.parseInt(colors[0]);
  124. int green = Integer.parseInt(colors[1]);
  125. int blue = Integer.parseInt(colors[2]);
  126. if (colors.length > 3) {
  127. int alpha = (int) (Double.parseDouble(colors[3]) * 255d);
  128. color = new Color(red, green, blue, alpha);
  129. } else {
  130. color = new Color(red, green, blue);
  131. }
  132. } else if (value.startsWith("hsl")) {
  133. // HSL color format hsl/hsla(100,50%,50%,1.0)
  134. String[] colors = value.substring(value.indexOf("(") + 1,
  135. value.length() - 1).split(",");
  136. int hue = Integer.parseInt(colors[0]);
  137. int saturation = Integer.parseInt(colors[1]
  138. .replace("%", ""));
  139. int lightness = Integer
  140. .parseInt(colors[2].replace("%", ""));
  141. int rgb = Color.HSLtoRGB(hue, saturation, lightness);
  142. if (colors.length > 3) {
  143. int alpha = (int) (Double.parseDouble(colors[3]) * 255d);
  144. color = new Color(rgb);
  145. color.setAlpha(alpha);
  146. } else {
  147. color = new Color(rgb);
  148. }
  149. }
  150. oldValue = value;
  151. fireEvent(new ColorChangeEvent((Component) field.getData(),
  152. color));
  153. }
  154. } catch (NumberFormatException nfe) {
  155. // Revert value
  156. field.setValue(oldValue);
  157. }
  158. }
  159. /**
  160. * Called when the component is refreshing
  161. */
  162. @Override
  163. protected String getCss(Component c) {
  164. return "background: " + color.getCSS();
  165. }
  166. }