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

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