source: josm/trunk/src/org/openstreetmap/josm/tools/ColorHelper.java @ 5241

Revision 3857, 1.2 KB checked in by bastiK, 16 months ago (diff)

forgot to add image

  • Property svn:eol-style set to native
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.awt.Color;
5
6/**
7 * Helper to convert from color to html string and back
8 */
9public 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        if (col == null)
34            return null;
35        return "#"+int2hex(col.getRed())+int2hex(col.getGreen())+int2hex(col.getBlue());
36    }
37}
Note: See TracBrowser for help on using the repository browser.