1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Helper to convert from color to HTML string and back.
|
---|
8 | */
|
---|
9 | public final class ColorHelper {
|
---|
10 |
|
---|
11 | private ColorHelper() {
|
---|
12 | // Hide default constructor for utils classes
|
---|
13 | }
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Returns the {@code Color} for the given HTML code.
|
---|
17 | * @param html the color code
|
---|
18 | * @return the color
|
---|
19 | */
|
---|
20 | public static Color html2color(String html) {
|
---|
21 | if (!html.isEmpty() && html.charAt(0) == '#')
|
---|
22 | html = html.substring(1);
|
---|
23 | if (html.length() == 3) {
|
---|
24 | html = new String(new char[]{
|
---|
25 | html.charAt(0), html.charAt(0),
|
---|
26 | html.charAt(1), html.charAt(1),
|
---|
27 | html.charAt(2), html.charAt(2)});
|
---|
28 | }
|
---|
29 | if (html.length() != 6 && html.length() != 8)
|
---|
30 | return null;
|
---|
31 | try {
|
---|
32 | return new Color(
|
---|
33 | Integer.parseInt(html.substring(0, 2), 16),
|
---|
34 | Integer.parseInt(html.substring(2, 4), 16),
|
---|
35 | Integer.parseInt(html.substring(4, 6), 16),
|
---|
36 | html.length() == 8 ? Integer.parseInt(html.substring(6, 8), 16) : 255);
|
---|
37 | } catch (NumberFormatException e) {
|
---|
38 | return null;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Returns the HTML color code (6 or 8 digit).
|
---|
44 | * @param col The color to convert
|
---|
45 | * @return the HTML color code (6 or 8 digit)
|
---|
46 | */
|
---|
47 | public static String color2html(Color col) {
|
---|
48 | return color2html(col, true);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Returns the HTML color code (6 or 8 digit).
|
---|
53 | * @param color The color to convert
|
---|
54 | * @param withAlpha if {@code true} and alpha value < 255, return 8-digit color code, else always 6-digit
|
---|
55 | * @return the HTML color code (6 or 8 digit)
|
---|
56 | * @since 6655
|
---|
57 | */
|
---|
58 | public static String color2html(Color color, boolean withAlpha) {
|
---|
59 | if (color == null)
|
---|
60 | return null;
|
---|
61 | int alpha = color.getAlpha();
|
---|
62 | return withAlpha && alpha != 255
|
---|
63 | ? String.format("#%06X%02X", color.getRGB() & 0x00ffffff, alpha)
|
---|
64 | : String.format("#%06X", color.getRGB() & 0x00ffffff);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Determines the correct foreground color (black or white) to use for the given background,
|
---|
69 | * so the text will be readable.
|
---|
70 | * @param bg background color
|
---|
71 | * @return {@code Color#BLACK} or {@code Color#WHITE}
|
---|
72 | * @since 9223
|
---|
73 | */
|
---|
74 | public static Color getForegroundColor(Color bg) {
|
---|
75 | // http://stackoverflow.com/a/3943023/2257172
|
---|
76 | return bg == null ? null :
|
---|
77 | (bg.getRed()*0.299 + bg.getGreen()*0.587 + bg.getBlue()*0.114) > 186 ?
|
---|
78 | Color.BLACK : Color.WHITE;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * convert float range 0 <= x <= 1 to integer range 0..255
|
---|
83 | * when dealing with colors and color alpha value
|
---|
84 | * @param val float value between 0 and 1
|
---|
85 | * @return null if val is null, the corresponding int if val is in the
|
---|
86 | * range 0...1. If val is outside that range, return 255
|
---|
87 | */
|
---|
88 | public static Integer float2int(Float val) {
|
---|
89 | if (val == null)
|
---|
90 | return null;
|
---|
91 | if (val < 0 || val > 1)
|
---|
92 | return 255;
|
---|
93 | return (int) (255f * val + 0.5f);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * convert integer range 0..255 to float range 0 <= x <= 1
|
---|
98 | * when dealing with colors and color alpha value
|
---|
99 | * @param val integer value
|
---|
100 | * @return corresponding float value in range 0 <= x <= 1
|
---|
101 | */
|
---|
102 | public static Float int2float(Integer val) {
|
---|
103 | if (val == null)
|
---|
104 | return null;
|
---|
105 | if (val < 0 || val > 255)
|
---|
106 | return 1f;
|
---|
107 | return ((float) val) / 255f;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Multiply the alpha value of the given color with the factor. The alpha value is clamped to 0..255
|
---|
112 | * @param color The color
|
---|
113 | * @param alphaFactor The factor to multiply alpha with.
|
---|
114 | * @return The new color.
|
---|
115 | * @since 11692
|
---|
116 | */
|
---|
117 | public static Color alphaMultiply(Color color, float alphaFactor) {
|
---|
118 | int alpha = float2int(int2float(color.getAlpha()) * alphaFactor);
|
---|
119 | alpha = Utils.clamp(alpha, 0, 255);
|
---|
120 | return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Returns the complementary color of {@code clr}.
|
---|
125 | * @param clr the color to complement
|
---|
126 | * @return the complementary color of {@code clr}
|
---|
127 | */
|
---|
128 | public static Color complement(Color clr) {
|
---|
129 | return new Color(255 - clr.getRed(), 255 - clr.getGreen(), 255 - clr.getBlue(), clr.getAlpha());
|
---|
130 | }
|
---|
131 | }
|
---|