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

Last change on this file since 6617 was 6380, checked in by Don-vip, 10 years ago

update license/copyright information

  • Property svn:eol-style set to native
File size: 1.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
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 final class ColorHelper {
10
11 private ColorHelper() {
12 // Hide default constructor for utils classes
13 }
14
15 public static Color html2color(String html) {
16 if (html.length() > 0 && html.charAt(0) == '#')
17 html = html.substring(1);
18 if (html.length() != 6 && html.length() != 8)
19 return null;
20 try {
21 return new Color(
22 Integer.parseInt(html.substring(0,2),16),
23 Integer.parseInt(html.substring(2,4),16),
24 Integer.parseInt(html.substring(4,6),16),
25 (html.length() == 8 ? Integer.parseInt(html.substring(6,8),16) : 255));
26 } catch (NumberFormatException e) {
27 return null;
28 }
29 }
30
31 private static String int2hex(int i) {
32 String s = Integer.toHexString(i / 16) + Integer.toHexString(i % 16);
33 return s.toUpperCase();
34 }
35
36 public static String color2html(Color col) {
37 if (col == null)
38 return null;
39 return "#"+int2hex(col.getRed())+int2hex(col.getGreen())+int2hex(col.getBlue());
40 }
41}
Note: See TracBrowser for help on using the repository browser.