KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > steadystate > css > HexColor


1 /*
2  * HexColor.java
3  *
4  * Steady State CSS2 Parser
5  *
6  * Copyright (C) 1999, 2002 Steady State Software Ltd. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * To contact the authors of the library, write to Steady State Software Ltd.,
23  * 49 Littleworth, Wing, Buckinghamshire, LU7 0JX, England
24  *
25  * http://www.steadystate.com/css/
26  * mailto:css@steadystate.co.uk
27  *
28  * $Id: HexColor.java,v 1.1.1.1 2003/12/28 21:22:43 davidsch Exp $
29  */

30
31 package com.steadystate.css;
32
33 /**
34  * @author David Schweinsberg
35  * @version $Release$
36  */

37 public class HexColor {
38
39     private int r = 0;
40     private int g = 0;
41     private int b = 0;
42
43     public HexColor(String JavaDoc hex) {
44
45         // Step past the hash at the beginning
46
int i = 0;
47         if (hex.charAt( 0 ) == '#') {
48             i++;
49         }
50
51         int len = hex.length() - i;
52         if (len == 3) {
53             r = Integer.parseInt(hex.substring(i + 0, i + 1), 16);
54             g = Integer.parseInt(hex.substring(i + 1, i + 2), 16);
55             b = Integer.parseInt(hex.substring(i + 2, i + 3), 16);
56             r = (r << 4) | r;
57             g = (g << 4) | g;
58             b = (b << 4) | b;
59         } else if(len == 6) {
60             r = Integer.parseInt(hex.substring(i + 0, i + 2), 16);
61             g = Integer.parseInt(hex.substring(i + 2, i + 4), 16);
62             b = Integer.parseInt(hex.substring(i + 4, i + 6), 16);
63         }
64     }
65
66     int getRed() {
67         return r;
68     }
69
70     int getGreen() {
71         return g;
72     }
73
74     int getBlue() {
75         return b;
76     }
77 }
78
Popular Tags