KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > util > HtmlGradient


1 /*
2  * $Id: HtmlGradient.java,v 1.3 2005/06/07 12:32:27 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitriy Belov <bel@jresearch.org>
23  * .
24  * * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on 11.07.2004
27  *
28  */

29 package org.jresearch.gossip.util;
30
31 import java.util.Map JavaDoc;
32
33 import org.tiling.memo.LruCacheFactory;
34
35 /**
36  * @author Dmitry Belov
37  *
38  */

39 public class HtmlGradient {
40
41     private static Map JavaDoc resultCache = new LruCacheFactory(1000).createCache();
42
43     private final static String JavaDoc ASTR = "*";
44
45     private final static String JavaDoc TABLE_START1 = "<table style=\"empty-cells: show;\" cellspacing=\"0\" cellpadding=\"0\" height=\"";
46
47     private final static String JavaDoc TABLE_START2 = "\" width=\"";
48
49     private final static String JavaDoc TABLE_START3 = "\" >";
50
51     private final static String JavaDoc TABLE_END = "</table>";
52
53     private final static String JavaDoc TR_START = "<tr>";
54
55     private final static String JavaDoc TR_END = "</tr>";
56
57     private final static String JavaDoc TD_START = "<td width=\"2\" bgcolor=\"";
58
59     private final static String JavaDoc TD_END = "\"></td>";
60
61     private final static String JavaDoc SIZE = "100%";
62
63     /**
64      * Comment for <code>DIRECTION_V</code>
65      */

66     public final static int DIRECTION_V = 0;
67
68     /**
69      * Comment for <code>DIRECTION_H</code>
70      */

71     public final static int DIRECTION_H = 1;
72
73     /**
74      * @param startColor
75      * @param endColor
76      * @param dens
77      * @param height
78      * @return
79      */

80     public static String JavaDoc makeHGradientHtml(String JavaDoc startColor, String JavaDoc endColor,
81             int dens, String JavaDoc height) {
82
83         return makeGradientHtml(startColor, endColor, dens, height, DIRECTION_H);
84
85     }
86
87     /**
88      * @param startColor
89      * @param endColor
90      * @param dens
91      * @param width
92      * @return
93      */

94     public static String JavaDoc makeVGradientHtml(String JavaDoc startColor, String JavaDoc endColor,
95             int dens, String JavaDoc width) {
96         return makeGradientHtml(startColor, endColor, dens, width, DIRECTION_V);
97     }
98
99     /**
100      * @param startColor
101      * @param endColor
102      * @param dens
103      * @param size
104      * @param direction
105      * @return
106      */

107     public static String JavaDoc makeGradientHtml(String JavaDoc startColor, String JavaDoc endColor,
108             int dens, String JavaDoc size, int direction) {
109         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
110         key.append(startColor);
111         key.append(ASTR);
112         key.append(endColor);
113         key.append(ASTR);
114         key.append(dens);
115         key.append(ASTR);
116         key.append(size);
117         key.append(ASTR);
118         key.append(direction);
119         Object JavaDoc value = resultCache.get(key);
120
121         if ((value == null) && !resultCache.containsKey(key)) {
122             String JavaDoc height = SIZE;
123             String JavaDoc width = SIZE;
124             String JavaDoc cell_start = TD_START;
125             String JavaDoc cell_end = TD_END;
126             switch (direction) {
127             case DIRECTION_V:
128                 width = size;
129                 cell_start = TR_START + cell_start;
130                 cell_end += TR_END;
131                 break;
132             case DIRECTION_H:
133                 height = size;
134                 break;
135             }
136
137             int[] sColor = { Integer.parseInt(startColor.substring(0, 2), 16),
138                     Integer.parseInt(startColor.substring(2, 4), 16),
139                     Integer.parseInt(startColor.substring(4), 16) };
140             int[] cDelta = {
141                     Integer.parseInt(endColor.substring(0, 2), 16) - sColor[0],
142                     Integer.parseInt(endColor.substring(2, 4), 16) - sColor[1],
143                     Integer.parseInt(endColor.substring(4), 16) - sColor[2] };
144
145             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
146             result.append(TABLE_START1);
147             result.append(height);
148             result.append(TABLE_START2);
149             result.append(width);
150             result.append(TABLE_START3);
151             if (direction == DIRECTION_H) {
152                 result.append(TR_START);
153             }
154
155             for (int i = 0; i < dens; i++) {
156                 result.append(cell_start);
157                 result.append(Integer.toHexString(sColor[0]
158                         + Math.round(i * cDelta[0] / dens)));
159                 result.append(Integer.toHexString(sColor[1]
160                         + Math.round(i * cDelta[1] / dens)));
161                 result.append(Integer.toHexString(sColor[2]
162                         + Math.round(i * cDelta[2] / dens)));
163                 result.append(cell_end);
164             }
165             if (direction == DIRECTION_H) {
166                 result.append(TR_END);
167             }
168             result.append(TABLE_END);
169             value = result.toString();
170             resultCache.put(key, value);
171         }
172
173         return (String JavaDoc) value;
174     }
175 }
Popular Tags