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.

ColorPickerGradient.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.Objects;
  18. import com.vaadin.shared.ui.colorpicker.Color;
  19. import com.vaadin.shared.ui.colorpicker.ColorPickerGradientServerRpc;
  20. import com.vaadin.shared.ui.colorpicker.ColorPickerGradientState;
  21. import com.vaadin.ui.AbstractColorPicker.Coordinates2Color;
  22. import com.vaadin.ui.AbstractField;
  23. /**
  24. * A component that represents a color gradient within a color picker.
  25. *
  26. * @since 7.0.0
  27. */
  28. public class ColorPickerGradient extends AbstractField<Color> {
  29. /** The converter. */
  30. private Coordinates2Color converter;
  31. /** The foreground color. */
  32. private Color color;
  33. private ColorPickerGradientServerRpc rpc = (cursorX,
  34. cursorY) -> setValue(converter.calculate(cursorX, cursorY), true);
  35. private ColorPickerGradient() {
  36. registerRpc(rpc);
  37. // width and height must be set here instead of in theme, otherwise
  38. // coordinate calculations fail
  39. getState().width = "220px";
  40. getState().height = "220px";
  41. }
  42. /**
  43. * Instantiates a new color picker gradient.
  44. *
  45. * @param id
  46. * the id
  47. * @param converter
  48. * the converter
  49. */
  50. public ColorPickerGradient(String id, Coordinates2Color converter) {
  51. this();
  52. addStyleName(id);
  53. this.converter = converter;
  54. }
  55. /**
  56. * Sets the value of this object. If the new value is not equal to
  57. * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
  58. * {@code NullPointerException} if the value is null.
  59. *
  60. * @param color
  61. * the new color, not {@code null}
  62. * @throws NullPointerException
  63. * if {@code color} is {@code null}
  64. */
  65. @Override
  66. public void setValue(Color color) {
  67. Objects.requireNonNull(color, "value must not be null");
  68. super.setValue(color);
  69. }
  70. @Override
  71. public Color getValue() {
  72. return color;
  73. }
  74. @Override
  75. protected void doSetValue(Color color) {
  76. this.color = color;
  77. int[] coords = converter.calculate(color);
  78. getState().cursorX = coords[0];
  79. getState().cursorY = coords[1];
  80. }
  81. /**
  82. * Sets the background color.
  83. *
  84. * @param color
  85. * the new background color
  86. */
  87. public void setBackgroundColor(Color color) {
  88. getState().bgColor = color.getCSS();
  89. }
  90. @Override
  91. protected ColorPickerGradientState getState() {
  92. return (ColorPickerGradientState) super.getState();
  93. }
  94. @Override
  95. protected ColorPickerGradientState getState(boolean markAsDirty) {
  96. return (ColorPickerGradientState) super.getState(markAsDirty);
  97. }
  98. }