Line | |
---|
1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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 class ColorHelper {
|
---|
10 |
|
---|
11 | public static Color html2color(String html) {
|
---|
12 | if (html.length() > 0 && html.charAt(0) == '#')
|
---|
13 | html = html.substring(1);
|
---|
14 | if (html.length() != 6 && html.length() != 8)
|
---|
15 | return null;
|
---|
16 | try {
|
---|
17 | return new Color(
|
---|
18 | Integer.parseInt(html.substring(0,2),16),
|
---|
19 | Integer.parseInt(html.substring(2,4),16),
|
---|
20 | Integer.parseInt(html.substring(4,6),16),
|
---|
21 | (html.length() == 8 ? Integer.parseInt(html.substring(6,8),16) : 255));
|
---|
22 | } catch (NumberFormatException e) {
|
---|
23 | return null;
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | private static String int2hex(int i) {
|
---|
28 | String s = Integer.toHexString(i / 16) + Integer.toHexString(i % 16);
|
---|
29 | return s.toUpperCase();
|
---|
30 | }
|
---|
31 |
|
---|
32 | public static String color2html(Color col) {
|
---|
33 | return "#"+int2hex(col.getRed())+int2hex(col.getGreen())+int2hex(col.getBlue());
|
---|
34 | }
|
---|
35 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.