ColorRGB a = new ColorRGB(0,0,0); ColorRGB b = a; a.r = 255;
/** * Gets the color as a packed integer, 8 bits per color component. HSB is * alpha, next is red, then green, and finally blue is LSB. */ public int getARGB() { final int a = getAlpha(); final int r = getRed(); final int g = getGreen(); final int b = getBlue(); return (a << 24) | (r << 16) | (g << 8) | b; }
private static ColorTable8 spectrum() { final byte[] r = new byte[256], g = new byte[256], b = new byte[256]; for (int i = 0; i < 256; i++) { final ColorRGB c = ColorRGB.fromHSVColor(i / 255d, 1d, 1d); r[i] = (byte) c.getRed(); g[i] = (byte) c.getGreen(); b[i] = (byte) c.getBlue(); } return new ColorTable8(r, g, b); }
@Test public void testFromHTMLColor() { assertEquals(new ColorRGB(1, 2, 3), ColorRGB.fromHTMLColor("#010203")); assertEquals(new ColorRGB(255, 0, 0), ColorRGB.fromHTMLColor("red")); assertSame(Colors.RED, ColorRGB.fromHTMLColor("red")); assertNotSame(Colors.RED, ColorRGB.fromHTMLColor("green")); }
@Test public void testToHTMLColor() { assertEquals("#010203", new ColorRGB(1, 2, 3).toHTMLColor()); assertEquals("red", new ColorRGB(255, 0, 0).toHTMLColor()); assertEquals("red", Colors.RED.toHTMLColor()); }
/** * Parses a color from the given string. The following formats are supported: * <ol> * <li>HTML color codes starting with hash ({@code #}), as handled by * {@link #fromHTMLColor(String)}.</li> * <li>Color presets, as handled by {@link Colors#getColor(String)}.</li> * <li>Integer triples of the form {@code r,g,b}, with each element in the * range {@code [0, 255]}.</li> * </ol> */ public ColorRGB(final String s) { final ColorRGB result = fromHTMLColor(s); if (result != null) { red = result.red; green = result.green; blue = result.blue; } else { final String[] tokens = s.split(","); red = parse(tokens, 0); green = parse(tokens, 1); blue = parse(tokens, 2); } }
/** Gets the name of the preset matching the given color. */ public static String getName(final ColorRGB color) { if (color == null) return null; for (final String name : COLORS.keySet()) { final ColorRGB value = COLORS.get(name); if (color.equals(value)) return name; } return null; }
@Override public int hashCode() { return getARGB(); }
/** * Parses a color from the given string. The following formats are supported: * <ol> * <li>HTML color codes starting with hash ({@code #}), as handled by * {@link #fromHTMLColor(String)}.</li> * <li>Color presets, as handled by {@link Colors#getColor(String)}.</li> * <li>Integer triples of the form {@code r,g,b}, with each element in the * range {@code [0, 255]}.</li> * </ol> */ public ColorRGB(final String s) { final ColorRGB result = fromHTMLColor(s); if (result != null) { red = result.red; green = result.green; blue = result.blue; } else { final String[] tokens = s.split(","); red = parse(tokens, 0); green = parse(tokens, 1); blue = parse(tokens, 2); } }
/** Gets the name of the preset matching the given color. */ public static String getName(final ColorRGB color) { if (color == null) return null; for (final String name : COLORS.keySet()) { final ColorRGB value = COLORS.get(name); if (color.equals(value)) return name; } return null; }
@Override public int hashCode() { return getARGB(); }
@Override public void setIconPixel(final int x, final int y, final ColorRGB color) { final Color awtColor = new Color(color.getRed(), color.getGreen(), color.getBlue()); // TODO this would be nice but doesn't work // button.getGraphics().setColor(awtColor); // button.getGraphics().drawLine(x, y, x, y); // But using an old reference does work graphics.setColor(awtColor); graphics.drawLine(x, y, x, y); }