|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2014 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.util; |
| 33 | + |
| 34 | +import java.io.Serializable; |
| 35 | + |
| 36 | +/** |
| 37 | + * A color with red, green and blue color components. |
| 38 | + * <p> |
| 39 | + * It exists mainly to avoid AWT references to {@link java.awt.Color}. |
| 40 | + * </p> |
| 41 | + * |
| 42 | + * @author Curtis Rueden |
| 43 | + * @author Lee Kamentsky |
| 44 | + * @author Barry DeZonia |
| 45 | + */ |
| 46 | +public class ColorRGB implements Serializable { |
| 47 | + |
| 48 | + private static final long serialVersionUID = 1L; |
| 49 | + |
| 50 | + private final int red; |
| 51 | + private final int green; |
| 52 | + private final int blue; |
| 53 | + |
| 54 | + public ColorRGB(final int red, final int green, final int blue) { |
| 55 | + this.red = red; |
| 56 | + this.green = green; |
| 57 | + this.blue = blue; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Parses a color from the given string. The following formats are supported: |
| 62 | + * <ol> |
| 63 | + * <li>HTML color codes starting with hash ({@code #}), as handled by |
| 64 | + * {@link #fromHTMLColor(String)}.</li> |
| 65 | + * <li>Color presets, as handled by {@link Colors#getColor(String)}.</li> |
| 66 | + * <li>Integer triples of the form {@code r,g,b}, with each element in the |
| 67 | + * range {@code [0, 255]}.</li> |
| 68 | + * </ol> |
| 69 | + */ |
| 70 | + public ColorRGB(final String s) { |
| 71 | + final ColorRGB result = fromHTMLColor(s); |
| 72 | + if (result != null) { |
| 73 | + red = result.red; |
| 74 | + green = result.green; |
| 75 | + blue = result.blue; |
| 76 | + } |
| 77 | + else { |
| 78 | + final String[] tokens = s.split(","); |
| 79 | + red = parse(tokens, 0); |
| 80 | + green = parse(tokens, 1); |
| 81 | + blue = parse(tokens, 2); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public int getRed() { |
| 86 | + return red; |
| 87 | + } |
| 88 | + |
| 89 | + public int getGreen() { |
| 90 | + return green; |
| 91 | + } |
| 92 | + |
| 93 | + public int getBlue() { |
| 94 | + return blue; |
| 95 | + } |
| 96 | + |
| 97 | + public int getAlpha() { |
| 98 | + return 0xff; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Gets the color as a packed integer, 8 bits per color component. HSB is |
| 103 | + * alpha, next is red, then green, and finally blue is LSB. |
| 104 | + */ |
| 105 | + public int getARGB() { |
| 106 | + final int a = getAlpha(); |
| 107 | + final int r = getRed(); |
| 108 | + final int g = getGreen(); |
| 109 | + final int b = getBlue(); |
| 110 | + return (a << 24) | (r << 16) | (g << 8) | b; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Convert this ColorRGB to a string in the format specified by <a |
| 115 | + * href="http://www.w3.org/TR/css3-color/">CSS Color Module Level 3 - W3C |
| 116 | + * Recommendation 07 June 2011</a>. |
| 117 | + * <p> |
| 118 | + * We preferentially encode using one of the colors in the table and fall back |
| 119 | + * to the hex encoding. |
| 120 | + * </p> |
| 121 | + * |
| 122 | + * @return HTML-encoded string |
| 123 | + */ |
| 124 | + public String toHTMLColor() { |
| 125 | + // return name of matching preset, if possible |
| 126 | + final String preset = Colors.getName(this); |
| 127 | + if (preset != null) return preset; |
| 128 | + |
| 129 | + // return hex-encoded string |
| 130 | + final int r = getRed(); |
| 131 | + final int g = getGreen(); |
| 132 | + final int b = getBlue(); |
| 133 | + return String.format("#%02x%02x%02x", r, g, b); |
| 134 | + } |
| 135 | + |
| 136 | + // -- Object methods -- |
| 137 | + |
| 138 | + @Override |
| 139 | + public String toString() { |
| 140 | + return red + "," + green + "," + blue; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean equals(final Object obj) { |
| 145 | + if (!(obj instanceof ColorRGB)) return super.equals(obj); |
| 146 | + final ColorRGB other = (ColorRGB) obj; |
| 147 | + return getRed() == other.getRed() && getGreen() == other.getGreen() && |
| 148 | + getBlue() == other.getBlue() && getAlpha() == other.getAlpha(); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public int hashCode() { |
| 153 | + return getARGB(); |
| 154 | + } |
| 155 | + |
| 156 | + // -- Static methods -- |
| 157 | + |
| 158 | + /** |
| 159 | + * Convert a string in the format specified by <a |
| 160 | + * href="http://www.w3.org/TR/css3-color/">CSS Color Module Level 3 - W3C |
| 161 | + * Recommendation 07 June 2011</a> to a {@link ColorRGB} object. |
| 162 | + * |
| 163 | + * @param color The color string to convert. |
| 164 | + * @return The resultant color object. |
| 165 | + */ |
| 166 | + public static ColorRGB fromHTMLColor(final String color) { |
| 167 | + // 4.2.1 |
| 168 | + // The format of an RGB value in hexadecimal notation is a "#" |
| 169 | + // immediately followed by either three or six hexadecimal characters. |
| 170 | + // The three-digit RGB notation (#rgb) is converted into six-digit form |
| 171 | + // (#rrggbb) by replicating digits, not by adding zeros. For example, |
| 172 | + // #fb0 expands to #ffbb00. This ensures that white (#ffffff) can be |
| 173 | + // specified with the short notation (#fff) and removes any dependencies |
| 174 | + // on the color depth of the display. |
| 175 | + if (color.startsWith("#")) { |
| 176 | + final String hexColor; |
| 177 | + if (color.length() == 4) { |
| 178 | + hexColor = |
| 179 | + new String(new char[] { color.charAt(0), color.charAt(1), |
| 180 | + color.charAt(1), color.charAt(2), color.charAt(2), |
| 181 | + color.charAt(3), color.charAt(3) }); |
| 182 | + } |
| 183 | + else hexColor = color; |
| 184 | + final int red = Integer.parseInt(hexColor.substring(1, 3), 16); |
| 185 | + final int green = Integer.parseInt(hexColor.substring(3, 5), 16); |
| 186 | + final int blue = Integer.parseInt(hexColor.substring(5, 7), 16); |
| 187 | + return new ColorRGB(red, green, blue); |
| 188 | + } |
| 189 | + // assume color is a preset |
| 190 | + return Colors.getColor(color); |
| 191 | + } |
| 192 | + |
| 193 | + // TODO - move when we handle color spaces. For now its a convenience method. |
| 194 | + |
| 195 | + public static ColorRGB fromHSVColor(final double h, final double s, |
| 196 | + final double v) |
| 197 | + { |
| 198 | + return hsvToRgb(h, s, v); |
| 199 | + } |
| 200 | + |
| 201 | + // -- Helper methods -- |
| 202 | + |
| 203 | + private int parse(final String[] s, final int index) { |
| 204 | + if (s == null || index >= s.length) return 0; |
| 205 | + try { |
| 206 | + return Integer.parseInt(s[index]); |
| 207 | + } |
| 208 | + catch (final NumberFormatException exc) { |
| 209 | + return 0; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Converts an HSV color value to RGB. |
| 215 | + * <p> |
| 216 | + * Assumes {@code h}, {@code s}, and {@code v} are contained in the set [0, 1] |
| 217 | + * and returns {@code r}, {@code g}, and {@code b} in the set [0, 255]. |
| 218 | + * </p> |
| 219 | + * <p> |
| 220 | + * Conversion formula adapted from Wikipedia's <a |
| 221 | + * href="http://en.wikipedia.org/wiki/HSL_and_HSV">HSL and HSV article</a> and |
| 222 | + * Michael Jackson's <a href="http://bit.ly/9L2qln">blog post on additive |
| 223 | + * color model conversion algorithms</a>. |
| 224 | + * </p> |
| 225 | + * |
| 226 | + * @param h The hue |
| 227 | + * @param s The saturation |
| 228 | + * @param v The value |
| 229 | + * @return ColorRGB The RGB representation |
| 230 | + */ |
| 231 | + private static ColorRGB hsvToRgb(final double h, final double s, |
| 232 | + final double v) |
| 233 | + { |
| 234 | + double r01 = 0, g01 = 0, b01 = 0; |
| 235 | + |
| 236 | + final int i = (int) Math.floor(h * 6); |
| 237 | + final double f = h * 6 - i; |
| 238 | + final double p = v * (1 - s); |
| 239 | + final double q = v * (1 - f * s); |
| 240 | + final double t = v * (1 - (1 - f) * s); |
| 241 | + |
| 242 | + switch (i % 6) { |
| 243 | + case 0: |
| 244 | + r01 = v; |
| 245 | + g01 = t; |
| 246 | + b01 = p; |
| 247 | + break; |
| 248 | + case 1: |
| 249 | + r01 = q; |
| 250 | + g01 = v; |
| 251 | + b01 = p; |
| 252 | + break; |
| 253 | + case 2: |
| 254 | + r01 = p; |
| 255 | + g01 = v; |
| 256 | + b01 = t; |
| 257 | + break; |
| 258 | + case 3: |
| 259 | + r01 = p; |
| 260 | + g01 = q; |
| 261 | + b01 = v; |
| 262 | + break; |
| 263 | + case 4: |
| 264 | + r01 = t; |
| 265 | + g01 = p; |
| 266 | + b01 = v; |
| 267 | + break; |
| 268 | + case 5: |
| 269 | + r01 = v; |
| 270 | + g01 = p; |
| 271 | + b01 = q; |
| 272 | + break; |
| 273 | + } |
| 274 | + |
| 275 | + final int r255 = (int) Math.round(r01 * 255); |
| 276 | + final int g255 = (int) Math.round(g01 * 255); |
| 277 | + final int b255 = (int) Math.round(b01 * 255); |
| 278 | + |
| 279 | + return new ColorRGB(r255, g255, b255); |
| 280 | + } |
| 281 | + |
| 282 | +} |
0 commit comments