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.

ColorPickerSelect.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.ui.components.colorpicker;
  17. import java.util.EnumSet;
  18. import com.vaadin.shared.ui.colorpicker.Color;
  19. import com.vaadin.ui.ComboBox;
  20. import com.vaadin.ui.Component;
  21. import com.vaadin.ui.CustomField;
  22. import com.vaadin.ui.VerticalLayout;
  23. /**
  24. * A component that represents color selection swatches within a color picker.
  25. *
  26. * @since 7.0.0
  27. */
  28. public class ColorPickerSelect extends CustomField<Color> {
  29. private ComboBox<ColorRange> range;
  30. private ColorPickerGrid grid;
  31. private enum ColorRange {
  32. ALL("All colors"), RED("Red colors"), GREEN("Green colors"), BLUE(
  33. "Blue colors");
  34. private final String caption;
  35. ColorRange(String caption) {
  36. this.caption = caption;
  37. }
  38. @Override
  39. public String toString() {
  40. return caption;
  41. }
  42. }
  43. @Override
  44. protected Component initContent() {
  45. VerticalLayout layout = new VerticalLayout();
  46. setStyleName("colorselect");
  47. setWidth("100%");
  48. range = new ComboBox<>(null, EnumSet.allOf(ColorRange.class));
  49. range.setEmptySelectionAllowed(false);
  50. range.setWidth("100%");
  51. range.addValueChangeListener(this::valueChange);
  52. range.setValue(ColorRange.ALL);
  53. layout.addComponent(range);
  54. grid = new ColorPickerGrid(createAllColors(14, 10));
  55. grid.setWidth("100%");
  56. grid.addValueChangeListener(this::fireEvent);
  57. layout.addComponent(grid);
  58. return layout;
  59. }
  60. /**
  61. * Creates the all colors.
  62. *
  63. * @param rows
  64. * the rows
  65. * @param columns
  66. * the columns
  67. *
  68. * @return the color[][]
  69. */
  70. private Color[][] createAllColors(int rows, int columns) {
  71. Color[][] colors = new Color[rows][columns];
  72. for (int row = 0; row < rows; row++) {
  73. for (int col = 0; col < columns; col++) {
  74. if (row < rows - 1) {
  75. // Create the color grid by varying the saturation and value
  76. // Calculate new hue value
  77. float hue = (float) col / (float) columns;
  78. float saturation = 1f;
  79. float value = 1f;
  80. // For the upper half use value=1 and variable
  81. // saturation
  82. if (row < rows / 2) {
  83. saturation = (row + 1f) / (rows / 2f);
  84. } else {
  85. value = 1f - (row - rows / 2f) / (rows / 2f);
  86. }
  87. colors[row][col] = new Color(
  88. Color.HSVtoRGB(hue, saturation, value));
  89. } else {
  90. // The last row should have the black&white gradient
  91. float hue = 0f;
  92. float saturation = 0f;
  93. float value = 1f - (float) col / (float) columns;
  94. colors[row][col] = new Color(
  95. Color.HSVtoRGB(hue, saturation, value));
  96. }
  97. }
  98. }
  99. return colors;
  100. }
  101. /**
  102. * Creates the color.
  103. *
  104. * @param color
  105. * the color
  106. * @param rows
  107. * the rows
  108. * @param columns
  109. * the columns
  110. *
  111. * @return the color[][]
  112. */
  113. private Color[][] createColors(Color color, int rows, int columns) {
  114. Color[][] colors = new Color[rows][columns];
  115. float[] hsv = color.getHSV();
  116. float hue = hsv[0];
  117. float saturation = 1f;
  118. float value = 1f;
  119. for (int row = 0; row < rows; row++) {
  120. for (int col = 0; col < columns; col++) {
  121. int index = row * columns + col;
  122. saturation = 1f;
  123. value = 1f;
  124. if (index <= rows * columns / 2) {
  125. saturation = index / ((float) rows * (float) columns / 2f);
  126. } else {
  127. index -= rows * columns / 2;
  128. value = 1f - index / ((float) rows * (float) columns / 2f);
  129. }
  130. colors[row][col] = new Color(
  131. Color.HSVtoRGB(hue, saturation, value));
  132. }
  133. }
  134. return colors;
  135. }
  136. private void valueChange(ValueChangeEvent<ColorRange> event) {
  137. if (grid == null) {
  138. return;
  139. }
  140. if (event.getValue() == ColorRange.ALL) {
  141. grid.setColorGrid(createAllColors(14, 10));
  142. } else if (event.getValue() == ColorRange.RED) {
  143. grid.setColorGrid(createColors(new Color(0xFF, 0, 0), 14, 10));
  144. } else if (event.getValue() == ColorRange.GREEN) {
  145. grid.setColorGrid(createColors(new Color(0, 0xFF, 0), 14, 10));
  146. } else if (event.getValue() == ColorRange.BLUE) {
  147. grid.setColorGrid(createColors(new Color(0, 0, 0xFF), 14, 10));
  148. }
  149. }
  150. /**
  151. * Returns the selected value.
  152. * <p>
  153. * Value can be {@code null} if component is not yet initialized via
  154. * {@link #initContent()}
  155. *
  156. * @see ColorPickerSelect#initContent()
  157. *
  158. * @return the selected color, may be {@code null}
  159. */
  160. @Override
  161. public Color getValue() {
  162. if (grid == null) {
  163. return null;
  164. }
  165. return grid.getValue();
  166. }
  167. @Override
  168. protected void doSetValue(Color value) {
  169. grid.setValue(value);
  170. }
  171. }