Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Color.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.shared.ui.colorpicker;
  17. import java.io.Serializable;
  18. /**
  19. * Default implementation for color.
  20. *
  21. * @since 7.0.0
  22. */
  23. @Deprecated
  24. public class Color implements Serializable {
  25. public static final Color WHITE = new Color(255, 255, 255);
  26. public static final Color BLACK = new Color(0, 0, 0);
  27. public static final Color RED = new Color(255, 0, 0);
  28. public static final Color GREEN = new Color(0, 255, 0);
  29. public static final Color BLUE = new Color(0, 0, 255);
  30. public static final Color YELLOW = new Color(255, 255, 0);
  31. public static final Color MAGENTA = new Color(255, 0, 255);
  32. public static final Color CYAN = new Color(0, 255, 255);
  33. private int red;
  34. private int green;
  35. private int blue;
  36. private int alpha;
  37. private String OUTOFRANGE = "Value must be within the range [0-255]. Was: ";
  38. /**
  39. * Creates a color that has the specified red, green, blue, and alpha values
  40. * within the range [0 - 255].
  41. *
  42. * @throws IllegalArgumentException
  43. * if <code>red</code>, <code>green</code>, <code>blue</code> or
  44. * <code>alpha</code> fall outside of the inclusive range from 0
  45. * to 255
  46. * @param red
  47. * the red value
  48. * @param green
  49. * the green value
  50. * @param blue
  51. * the blue value
  52. * @param alpha
  53. * the alpha value
  54. */
  55. public Color(int red, int green, int blue, int alpha) {
  56. checkRange(red, green, blue, alpha);
  57. this.red = red;
  58. this.green = green;
  59. this.blue = blue;
  60. this.alpha = alpha;
  61. }
  62. /**
  63. * Creates a color that has the specified red, green, and blue values within
  64. * the range [0 - 255]. Alpha gets the default value of 255.
  65. *
  66. * @throws IllegalArgumentException
  67. * if <code>red</code>, <code>green</code> or <code>blue</code>
  68. * fall outside of the inclusive range from 0 to 255
  69. * @param red
  70. * the red value
  71. * @param green
  72. * the green value
  73. * @param blue
  74. * the blue value
  75. */
  76. public Color(int red, int green, int blue) {
  77. this(red, green, blue, 255);
  78. }
  79. /**
  80. * Creates a color based on an RGB value.
  81. *
  82. * @throws IllegalArgumentException
  83. * if converted values of <code>red</code>, <code>green</code>,
  84. * <code>blue</code> or <code>alpha</code> fall outside of the
  85. * inclusive range from 0 to 255
  86. *
  87. * @param rgb
  88. * the RGB value
  89. */
  90. public Color(int rgb) {
  91. int value = 0xff000000 | rgb;
  92. int red = (value >> 16) & 0xFF;
  93. int green = (value >> 8) & 0xFF;
  94. int blue = (value >> 0) & 0xFF;
  95. int alpha = (value >> 24) & 0xff;
  96. checkRange(red, green, blue, alpha);
  97. this.red = red;
  98. this.green = green;
  99. this.blue = blue;
  100. this.alpha = alpha;
  101. }
  102. /**
  103. * Checks that all values are within the acceptable range of [0, 255].
  104. *
  105. * @throws IllegalArgumentException
  106. * if any of the values fall outside of the range
  107. *
  108. * @param red
  109. * @param green
  110. * @param blue
  111. * @param alpha
  112. */
  113. private void checkRange(int red, int green, int blue, int alpha) {
  114. if (!withinRange(red) || !withinRange(green) || !withinRange(blue)
  115. || !withinRange(alpha)) {
  116. String errorMessage = "All values must fall within range [0-255]. (red: "
  117. + red + ", green: " + green + ", blue: " + blue
  118. + ", alpha: " + alpha + ")";
  119. throw new IllegalArgumentException(errorMessage);
  120. }
  121. }
  122. /**
  123. * Checks whether the value is within the acceptable range of [0, 255].
  124. *
  125. * @param value
  126. * @return true if the value falls within the range, false otherwise
  127. */
  128. private boolean withinRange(int value) {
  129. if (value < 0 || value > 255) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. /**
  135. * Returns the red value of the color.
  136. *
  137. */
  138. public int getRed() {
  139. return red;
  140. }
  141. /**
  142. * Sets the red value of the color. Value must be within the range [0, 255].
  143. *
  144. * @param red
  145. * new red value
  146. */
  147. public void setRed(int red) {
  148. if (withinRange(red)) {
  149. this.red = red;
  150. } else {
  151. throw new IllegalArgumentException(OUTOFRANGE + red);
  152. }
  153. }
  154. /**
  155. * Returns the green value of the color.
  156. *
  157. */
  158. public int getGreen() {
  159. return green;
  160. }
  161. /**
  162. * Sets the green value of the color. Value must be within the range [0,
  163. * 255].
  164. *
  165. * @param green
  166. * new green value
  167. */
  168. public void setGreen(int green) {
  169. if (withinRange(green)) {
  170. this.green = green;
  171. } else {
  172. throw new IllegalArgumentException(OUTOFRANGE + green);
  173. }
  174. }
  175. /**
  176. * Returns the blue value of the color.
  177. *
  178. */
  179. public int getBlue() {
  180. return blue;
  181. }
  182. /**
  183. * Sets the blue value of the color. Value must be within the range [0,
  184. * 255].
  185. *
  186. * @param blue
  187. * new blue value
  188. */
  189. public void setBlue(int blue) {
  190. if (withinRange(blue)) {
  191. this.blue = blue;
  192. } else {
  193. throw new IllegalArgumentException(OUTOFRANGE + blue);
  194. }
  195. }
  196. /**
  197. * Returns the alpha value of the color.
  198. *
  199. */
  200. public int getAlpha() {
  201. return alpha;
  202. }
  203. /**
  204. * Sets the alpha value of the color. Value must be within the range [0,
  205. * 255].
  206. *
  207. * @param alpha
  208. * new alpha value
  209. */
  210. public void setAlpha(int alpha) {
  211. if (withinRange(alpha)) {
  212. this.alpha = alpha;
  213. } else {
  214. throw new IllegalArgumentException(OUTOFRANGE + alpha);
  215. }
  216. }
  217. /**
  218. * Returns CSS representation of the Color, e.g. #000000.
  219. */
  220. public String getCSS() {
  221. String redString = Integer.toHexString(red);
  222. redString = redString.length() < 2 ? "0" + redString : redString;
  223. String greenString = Integer.toHexString(green);
  224. greenString = greenString.length() < 2 ? "0" + greenString
  225. : greenString;
  226. String blueString = Integer.toHexString(blue);
  227. blueString = blueString.length() < 2 ? "0" + blueString : blueString;
  228. return "#" + redString + greenString + blueString;
  229. }
  230. /**
  231. * Returns RGB value of the color.
  232. */
  233. public int getRGB() {
  234. return ((alpha & 0xFF) << 24) | ((red & 0xFF) << 16)
  235. | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0);
  236. }
  237. /**
  238. * Returns converted HSV components of the color.
  239. *
  240. */
  241. public float[] getHSV() {
  242. float[] hsv = new float[3];
  243. int maxColor = (red > green) ? red : green;
  244. if (blue > maxColor) {
  245. maxColor = blue;
  246. }
  247. int minColor = (red < green) ? red : green;
  248. if (blue < minColor) {
  249. minColor = blue;
  250. }
  251. float value = maxColor / 255.0f;
  252. float saturation = 0;
  253. if (maxColor != 0) {
  254. saturation = ((float) (maxColor - minColor)) / ((float) maxColor);
  255. }
  256. float hue = 0;
  257. if (saturation != 0) {
  258. float redF = ((float) (maxColor - red))
  259. / ((float) (maxColor - minColor));
  260. float greenF = ((float) (maxColor - green))
  261. / ((float) (maxColor - minColor));
  262. float blueF = ((float) (maxColor - blue))
  263. / ((float) (maxColor - minColor));
  264. if (red == maxColor) {
  265. hue = blueF - greenF;
  266. } else if (green == maxColor) {
  267. hue = 2.0f + redF - blueF;
  268. } else {
  269. hue = 4.0f + greenF - redF;
  270. }
  271. hue = hue / 6.0f;
  272. if (hue < 0) {
  273. hue = hue + 1.0f;
  274. }
  275. }
  276. hsv[0] = hue;
  277. hsv[1] = saturation;
  278. hsv[2] = value;
  279. return hsv;
  280. }
  281. @Override
  282. public int hashCode() {
  283. return getRGB();
  284. }
  285. @Override
  286. public boolean equals(Object obj) {
  287. return obj instanceof Color && ((Color) obj).getRGB() == getRGB();
  288. }
  289. /**
  290. * <p>
  291. * Converts HSV's hue, saturation and value into an RGB value.
  292. * <p>
  293. * The <code>saturation</code> and <code>value</code> components should be
  294. * floating-point values within the range [0.0-1.0].
  295. * <p>
  296. *
  297. * @param hue
  298. * the hue of the color
  299. * @param saturation
  300. * the saturation of the color
  301. * @param value
  302. * the value of the color
  303. * @return the RGB value of corresponding color
  304. */
  305. public static int HSVtoRGB(float hue, float saturation, float value) {
  306. int red = 0;
  307. int green = 0;
  308. int blue = 0;
  309. if (saturation == 0) {
  310. red = green = blue = (int) (value * 255.0f + 0.5f);
  311. } else {
  312. float h = (hue - (float) Math.floor(hue)) * 6.0f;
  313. float f = h - (float) java.lang.Math.floor(h);
  314. float p = value * (1.0f - saturation);
  315. float q = value * (1.0f - saturation * f);
  316. float t = value * (1.0f - (saturation * (1.0f - f)));
  317. switch ((int) h) {
  318. case 0:
  319. red = (int) (value * 255.0f + 0.5f);
  320. green = (int) (t * 255.0f + 0.5f);
  321. blue = (int) (p * 255.0f + 0.5f);
  322. break;
  323. case 1:
  324. red = (int) (q * 255.0f + 0.5f);
  325. green = (int) (value * 255.0f + 0.5f);
  326. blue = (int) (p * 255.0f + 0.5f);
  327. break;
  328. case 2:
  329. red = (int) (p * 255.0f + 0.5f);
  330. green = (int) (value * 255.0f + 0.5f);
  331. blue = (int) (t * 255.0f + 0.5f);
  332. break;
  333. case 3:
  334. red = (int) (p * 255.0f + 0.5f);
  335. green = (int) (q * 255.0f + 0.5f);
  336. blue = (int) (value * 255.0f + 0.5f);
  337. break;
  338. case 4:
  339. red = (int) (t * 255.0f + 0.5f);
  340. green = (int) (p * 255.0f + 0.5f);
  341. blue = (int) (value * 255.0f + 0.5f);
  342. break;
  343. case 5:
  344. red = (int) (value * 255.0f + 0.5f);
  345. green = (int) (p * 255.0f + 0.5f);
  346. blue = (int) (q * 255.0f + 0.5f);
  347. break;
  348. }
  349. }
  350. return 0xff000000 | (red << 16) | (green << 8) | (blue << 0);
  351. }
  352. /**
  353. * <p>
  354. * Converts HSL's hue, saturation and lightness into an RGB value.
  355. *
  356. * @param hue
  357. * the hue of the color. The unit of the value is degrees and
  358. * should be between 0-360.
  359. * @param saturation
  360. * the saturation of the color. The unit of the value is
  361. * percentages and should be between 0-100;
  362. * @param lightness
  363. * the lightness of the color. The unit of the value is
  364. * percentages and should be between 0-100;
  365. *
  366. * @return the RGB value of corresponding color
  367. */
  368. public static int HSLtoRGB(int hue, int saturation, int lightness) {
  369. int red = 0;
  370. int green = 0;
  371. int blue = 0;
  372. float hueRatio = hue / 360f;
  373. float saturationRatio = saturation / 100f;
  374. float lightnessRatio = lightness / 100f;
  375. if (saturationRatio == 0) {
  376. red = green = blue = (int) (lightnessRatio * 255.0f + 0.5f);
  377. } else {
  378. float p = lightnessRatio < 0.5f
  379. ? lightnessRatio * (1f + saturationRatio)
  380. : lightnessRatio + saturationRatio
  381. - lightnessRatio * saturationRatio;
  382. float q = 2 * lightnessRatio - p;
  383. red = hslComponentToRgbComponent(p, q, hueRatio + (1f / 3f));
  384. green = hslComponentToRgbComponent(p, q, hueRatio);
  385. blue = hslComponentToRgbComponent(p, q, hueRatio - (1f / 3f));
  386. }
  387. return 0xff000000 | (red << 16) | (green << 8) | (blue << 0);
  388. }
  389. private static int hslComponentToRgbComponent(float p, float q,
  390. float ratio) {
  391. if (ratio < 0) {
  392. ratio += 1;
  393. } else if (ratio > 1) {
  394. ratio -= 1;
  395. }
  396. if (6 * ratio < 1f) {
  397. return (int) ((q + (p - q) * 6f * ratio) * 255f + 0.5f);
  398. } else if (2f * ratio < 1f) {
  399. return (int) (p * 255f + 0.5f);
  400. } else if (3f * ratio < 2f) {
  401. return (int) ((q + (p - q) * ((2f / 3f) - ratio) * 6f) * 255f
  402. + 0.5f);
  403. }
  404. return (int) (q * 255f + 0.5f);
  405. }
  406. }