themeinstaller/source/src/com/nokia/tools/themeinstaller/cssparser/ColorResolver.java
branchRCL_3
changeset 18 04b7640f6fb5
parent 0 05da4621cfb2
equal deleted inserted replaced
17:fe49e33862e2 18:04b7640f6fb5
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Color resolver for resolving colors set as strings.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.cssparser;
       
    20 
       
    21 import java.awt.Color;
       
    22 import java.util.Hashtable;
       
    23 
       
    24 /**
       
    25  * Color resolver for resolving colors set as strings. Corresponding
       
    26  * functionality is implemented in Xuikon CSS Parser (cssconstants.h and
       
    27  * CCSSLexicalUnit).
       
    28  *
       
    29  * Color resolver also includes HSL to RGB converting.
       
    30  *
       
    31  */
       
    32 public class ColorResolver extends Hashtable
       
    33     {
       
    34 
       
    35     // Default serial version UID
       
    36     private static final long serialVersionUID = 1L;
       
    37     private static final int RGB_MAX_VALUE = 255;
       
    38     private static final int PERCENTAGE_MAX_VALUE = 100;
       
    39 
       
    40     public ColorResolver()
       
    41         {
       
    42         // HTML4 color keywords
       
    43         put( "maroon", new Color( 128, 0, 0 ) );
       
    44         put( "red", new Color( 255, 0, 0 ) );
       
    45         put( "orange", new Color( 255, 165, 0 ) );
       
    46         put( "yellow", new Color( 255, 255, 0 ) );
       
    47         put( "olive", new Color( 128, 128, 0 ) );
       
    48         put( "purple", new Color( 128, 0, 128 ) );
       
    49         put( "fuchsia", new Color( 255, 0, 255 ) );
       
    50         put( "white", new Color( 255, 255, 255 ) );
       
    51         put( "lime", new Color( 0, 255, 0 ) );
       
    52         put( "green", new Color( 0, 128, 0 ) );
       
    53         put( "navy", new Color( 0, 0, 128 ) );
       
    54         put( "blue", new Color( 0, 0, 255 ) );
       
    55         put( "aqua", new Color( 0, 255, 255 ) );
       
    56         put( "teal", new Color( 0, 128, 128 ) );
       
    57         put( "black", new Color( 0, 0, 0 ) );
       
    58         put( "silver", new Color( 192, 192, 192 ) );
       
    59         put( "gray", new Color( 128, 128, 128 ) );
       
    60 
       
    61         // Transparent
       
    62         put( "transparent", new Color( 0, 0, 0, 0 ) );
       
    63 
       
    64         // SVG 1.0 COLOR KEYWORDS
       
    65         // (excl. the ones defined in the HTML4 colors set)
       
    66         put( "aliceblue", new Color( 240, 248, 255 ) );
       
    67         put( "antiquewhite", new Color( 250, 235, 215 ) );
       
    68         put( "aquamarine", new Color( 127, 255, 212 ) );
       
    69         put( "azure", new Color( 240, 255, 255 ) );
       
    70         put( "beige", new Color( 245, 245, 220 ) );
       
    71         put( "bisque", new Color( 255, 228, 196 ) );
       
    72         put( "blanchedalmond", new Color( 255, 235, 205 ) );
       
    73         put( "blueviolet", new Color( 138, 43, 226 ) );
       
    74         put( "brown", new Color( 165, 42, 42 ) );
       
    75         put( "burlywood", new Color( 222, 184, 135 ) );
       
    76         put( "cadetblue", new Color( 95, 158, 160 ) );
       
    77         put( "chartreuse", new Color( 127, 255, 0 ) );
       
    78         put( "chocolate", new Color( 210, 105, 30 ) );
       
    79         put( "coral", new Color( 255, 127, 80 ) );
       
    80         put( "cornflowerblue", new Color( 100, 149, 237 ) );
       
    81         put( "cornsilk", new Color( 255, 248, 220 ) );
       
    82         put( "crimson", new Color( 220, 20, 60 ) );
       
    83         put( "cyan", new Color( 0, 255, 255 ) );
       
    84         put( "darkblue", new Color( 0, 0, 139 ) );
       
    85         put( "darkcyan", new Color( 0, 139, 139 ) );
       
    86         put( "darkgoldenrod", new Color( 184, 134, 11 ) );
       
    87         put( "darkgray", new Color( 169, 169, 169 ) );
       
    88         put( "darkgrey", new Color( 169, 169, 169 ) );
       
    89         put( "darkgreen", new Color( 0, 100, 0 ) );
       
    90         put( "darkkhaki", new Color( 189, 183, 107 ) );
       
    91         put( "darkmagenta", new Color( 139, 0, 139 ) );
       
    92         put( "darkolivegreen", new Color( 85, 107, 47 ) );
       
    93         put( "darkorange", new Color( 255, 140, 0 ) );
       
    94         put( "darkorchid", new Color( 153, 50, 204 ) );
       
    95         put( "darkred", new Color( 139, 0, 0 ) );
       
    96         put( "darksalmon", new Color( 233, 150, 122 ) );
       
    97         put( "darkseagreen", new Color( 143, 188, 143 ) );
       
    98         put( "darkslateblue", new Color( 72, 61, 139 ) );
       
    99         put( "darkslategray", new Color( 47, 79, 79 ) );
       
   100         put( "darkslategrey", new Color( 47, 79, 79 ) );
       
   101         put( "darkturquoise", new Color( 0, 206, 209 ) );
       
   102         put( "darkviolet", new Color( 148, 0, 211 ) );
       
   103         put( "deeppink", new Color( 255, 20, 147 ) );
       
   104         put( "deepskyblue", new Color( 0, 191, 255 ) );
       
   105         put( "dimgray", new Color( 105, 105, 105 ) );
       
   106         put( "dimgrey", new Color( 105, 105, 105 ) );
       
   107         put( "dodgerblue", new Color( 30, 144, 255 ) );
       
   108         put( "firebrick", new Color( 178, 34, 34 ) );
       
   109         put( "floralwhite", new Color( 255, 250, 240 ) );
       
   110         put( "forestgreen", new Color( 34, 139, 34 ) );
       
   111         put( "fuchsia", new Color( 255, 0, 255 ) );
       
   112         put( "gainsboro", new Color( 220, 220, 220 ) );
       
   113         put( "ghostwhite", new Color( 248, 248, 255 ) );
       
   114         put( "gold", new Color( 255, 215, 0 ) );
       
   115         put( "goldenrod", new Color( 218, 165, 32 ) );
       
   116         put( "greenyellow", new Color( 173, 255, 47 ) );
       
   117         put( "grey", new Color( 240, 255, 240 ) );
       
   118         put( "honeydew", new Color( 240, 255, 240 ) );
       
   119         put( "hotpink", new Color( 255, 105, 180 ) );
       
   120         put( "indianred", new Color( 205, 92, 92 ) );
       
   121         put( "indigo", new Color( 75, 0, 130 ) );
       
   122         put( "ivory", new Color( 255, 255, 240 ) );
       
   123         put( "khaki", new Color( 240, 230, 140 ) );
       
   124         put( "lavender", new Color( 230, 230, 250 ) );
       
   125         put( "lavenderblush", new Color( 255, 240, 245 ) );
       
   126         put( "lawngreen", new Color( 124, 252, 0 ) );
       
   127         put( "lemonchiffon", new Color( 255, 250, 205 ) );
       
   128         put( "lightblue", new Color( 173, 216, 230 ) );
       
   129         put( "lightcoral", new Color( 240, 128, 128 ) );
       
   130         put( "lightcyan", new Color( 224, 255, 255 ) );
       
   131         put( "lightgoldenrodyellow", new Color( 250, 250, 210 ) );
       
   132         put( "lightgray", new Color( 211, 211, 211 ) );
       
   133         put( "lightgrey", new Color( 211, 211, 211 ) );
       
   134         put( "lightgreen", new Color( 144, 238, 144 ) );
       
   135         put( "lightpink", new Color( 255, 182, 193 ) );
       
   136         put( "lightsalmon", new Color( 255, 160, 122 ) );
       
   137         put( "lightseagreen", new Color( 32, 178, 170 ) );
       
   138         put( "lightskyblue", new Color( 135, 206, 250 ) );
       
   139         put( "lightslategray", new Color( 119, 136, 153 ) );
       
   140         put( "lightslategrey", new Color( 119, 136, 153 ) );
       
   141         put( "lightsteelblue", new Color( 176, 196, 222 ) );
       
   142         put( "lightyellow", new Color( 255, 255, 224 ) );
       
   143         put( "limegreen", new Color( 50, 205, 50 ) );
       
   144         put( "linen", new Color( 250, 240, 230 ) );
       
   145         put( "magenta", new Color( 255, 0, 255 ) );
       
   146         put( "mediumaquamarine", new Color( 102, 205, 170 ) );
       
   147         put( "mediumblue", new Color( 0, 0, 205 ) );
       
   148         put( "mediumorchid", new Color( 186, 85, 211 ) );
       
   149         put( "mediumpurple", new Color( 147, 112, 219 ) );
       
   150         put( "mediumseagreen", new Color( 60, 179, 113 ) );
       
   151         put( "mediumslateblue", new Color( 123, 104, 238 ) );
       
   152         put( "mediumspringgreen", new Color( 0, 250, 154 ) );
       
   153         put( "mediumturquoise", new Color( 72, 209, 204 ) );
       
   154         put( "mediumvioletred", new Color( 199, 21, 133 ) );
       
   155         put( "midnightblue", new Color( 25, 25, 112 ) );
       
   156         put( "mintcream", new Color( 245, 255, 250 ) );
       
   157         put( "mistyrose", new Color( 255, 228, 225 ) );
       
   158         put( "moccasin", new Color( 255, 228, 181 ) );
       
   159         put( "navajowhite", new Color( 255, 222, 173 ) );
       
   160         put( "oldlace", new Color( 253, 245, 230 ) );
       
   161         put( "olivedrab", new Color( 107, 142, 35 ) );
       
   162         put( "orangered", new Color( 255, 69, 0 ) );
       
   163         put( "orchid", new Color( 218, 112, 214 ) );
       
   164         put( "palegoldenrod", new Color( 238, 232, 170 ) );
       
   165         put( "palegreen", new Color( 152, 251, 152 ) );
       
   166         put( "paleturquoise", new Color( 175, 238, 238 ) );
       
   167         put( "palevioletred", new Color( 219, 112, 147 ) );
       
   168         put( "papayawhip", new Color( 255, 239, 213 ) );
       
   169         put( "peachpuff", new Color( 255, 218, 185 ) );
       
   170         put( "peru", new Color( 205, 133, 63 ) );
       
   171         put( "pink", new Color( 255, 192, 203 ) );
       
   172         put( "plum", new Color( 221, 160, 221 ) );
       
   173         put( "powderblue", new Color( 176, 224, 230 ) );
       
   174         put( "rosybrown", new Color( 188, 143, 143 ) );
       
   175         put( "royalblue", new Color( 65, 105, 225 ) );
       
   176         put( "saddlebrown", new Color( 139, 69, 19 ) );
       
   177         put( "salmon", new Color( 250, 128, 114 ) );
       
   178         put( "sandybrown", new Color( 244, 164, 96 ) );
       
   179         put( "seagreen", new Color( 46, 139, 87 ) );
       
   180         put( "seashell", new Color( 255, 245, 238 ) );
       
   181         put( "sienna", new Color( 160, 82, 45 ) );
       
   182         put( "skyblue", new Color( 135, 206, 235 ) );
       
   183         put( "slateblue", new Color( 106, 90, 205 ) );
       
   184         put( "slategray", new Color( 112, 128, 144 ) );
       
   185         put( "slategrey", new Color( 112, 128, 144 ) );
       
   186         put( "snow", new Color( 255, 250, 250 ) );
       
   187         put( "springgreen", new Color( 0, 255, 127 ) );
       
   188         put( "steelblue", new Color( 70, 130, 180 ) );
       
   189         put( "tan", new Color( 210, 180, 140 ) );
       
   190         put( "thistle", new Color( 216, 191, 216 ) );
       
   191         put( "tomato", new Color( 255, 99, 71 ) );
       
   192         put( "turquoise", new Color( 64, 224, 208 ) );
       
   193         put( "violet", new Color( 238, 130, 238 ) );
       
   194         put( "wheat", new Color( 245, 222, 179 ) );
       
   195         put( "whitesmoke", new Color( 245, 245, 245 ) );
       
   196         put( "yellowgreen", new Color( 154, 205, 50 ) );
       
   197         }
       
   198 
       
   199     /**
       
   200      * Gets the color value from percentage.
       
   201      *
       
   202      * For example:
       
   203      * 100% -> 255
       
   204      * 50%  -> 128
       
   205      * 0%   -> 0
       
   206      *
       
   207      * @param aPercentage the a percentage
       
   208      *
       
   209      * @return the color value from percentage
       
   210      */
       
   211     public int getColorValueFromPercentage( float aPercentage )
       
   212         {
       
   213         if ( aPercentage < 0 || PERCENTAGE_MAX_VALUE < aPercentage )
       
   214             {
       
   215             throw new IllegalStateException( "Color percentage out of range" );
       
   216             }
       
   217         float value = RGB_MAX_VALUE * ( ( aPercentage ) / PERCENTAGE_MAX_VALUE );
       
   218         return ( int ) ( value + 0.5f );
       
   219         }
       
   220 
       
   221     /**
       
   222      * Translate color from HSL to RGB.
       
   223      *
       
   224      * The algorithm is taken from
       
   225      * CSS3 Color Module:
       
   226      * http://www.w3.org/TR/css3-color/:
       
   227      *
       
   228      * @param aHue Hue
       
   229      * @param aSaturation Saturation
       
   230      * @param aLightness Lightness
       
   231      *
       
   232      * @return the color
       
   233      */
       
   234     public Color hslToRgb( float aHue, float aSaturation, float aLightness )
       
   235         {
       
   236 
       
   237         /*
       
   238           In these algorithms, all
       
   239           three values (H, S and L) have been normalized to fractions 0..1:
       
   240 
       
   241               HOW TO RETURN hsl.to.rgb(h, s, l):
       
   242               SELECT:
       
   243               l<=0.5: PUT l*(s+1) IN m2
       
   244               ELSE: PUT l+s-l*s IN m2
       
   245                PUT l*2-m2 IN m1
       
   246                PUT hue.to.rgb(m1, m2, h+1/3) IN r
       
   247                PUT hue.to.rgb(m1, m2, h    ) IN g
       
   248                PUT hue.to.rgb(m1, m2, h-1/3) IN b
       
   249                RETURN (r, g, b)
       
   250 
       
   251               HOW TO RETURN hue.to.rgb(m1, m2, h):
       
   252                IF h<0: PUT h+1 IN h
       
   253                IF h>1: PUT h-1 IN h
       
   254                IF h*6<1: RETURN m1+(m2-m1)*h*6
       
   255                IF h*2<1: RETURN m2
       
   256                IF h*3<2: RETURN m1+(m2-m1)*(2/3-h)*6
       
   257                RETURN m1
       
   258 
       
   259 
       
   260          */
       
   261 
       
   262         float h = aHue;
       
   263         h /= 360;
       
   264 
       
   265         if ( aSaturation < 0 || 100 < aSaturation
       
   266                 || aLightness < 0 || 100 < aLightness )
       
   267             {
       
   268             throw new IllegalStateException( "HSL value out of range" );
       
   269             }
       
   270 
       
   271         float s = aSaturation;
       
   272         s /= 100;
       
   273 
       
   274         float l = aLightness;
       
   275         l /= 100;
       
   276 
       
   277         float[] color = { 0, 0, 0 };
       
   278 
       
   279         // If Saturation is zero, return gray with correct lightness
       
   280         if ( aSaturation == 0 )
       
   281             {
       
   282             int e = ( int ) ( RGB_MAX_VALUE * l + 0.5f );
       
   283             return new Color( e, e, e );
       
   284             }
       
   285 
       
   286         // HOW TO RETURN hsl.to.rgb(h, s, l):
       
   287         // SELECT:
       
   288         // l<=0.5: PUT l*(s+1) IN m2
       
   289         // ELSE: PUT l+s-l*s IN m2
       
   290         float m2 = 0;
       
   291 
       
   292         if ( l < 0.5 )
       
   293             {
       
   294             m2 = l * ( 1.0f + s );
       
   295             }
       
   296 
       
   297         if ( l >= 0.5 )
       
   298             {
       
   299             m2 = l + s - l * s;
       
   300             }
       
   301 
       
   302         // temp1 = 2.0*L - temp2
       
   303         // PUT l*2-m2 IN m1
       
   304         float m1 = 2.0f * l - m2;
       
   305 
       
   306         // PUT hue.to.rgb(m1, m2, h+1/3) IN r
       
   307         // PUT hue.to.rgb(m1, m2, h ) IN g
       
   308         // PUT hue.to.rgb(m1, m2, h-1/3) IN b
       
   309         float tempRGB[] = { h + 1.0f / 3.0f, h, h - 1.0f / 3.0f };
       
   310 
       
   311         for ( int i = 0; i < 3; i++ )
       
   312             {
       
   313             // HOW TO RETURN hue.to.rgb(m1, m2, h):
       
   314             // IF h<0: PUT h+1 IN h
       
   315             if ( tempRGB[ i ] < 0 )
       
   316                 {
       
   317                 tempRGB[ i ] = tempRGB[ i ] + 1.0f;
       
   318                 }
       
   319             // IF h>1: PUT h-1 IN h
       
   320             if ( tempRGB[ i ] > 1 )
       
   321                 {
       
   322                 tempRGB[ i ] = tempRGB[ i ] - 1.0f;
       
   323                 }
       
   324 
       
   325             // HOW TO RETURN hue.to.rgb(m1, m2, h):
       
   326             // IF h*6<1: RETURN m1+(m2-m1)*h*6
       
   327             if ( 6.0f * tempRGB[ i ] < 1.0f )
       
   328                 {
       
   329                 color[ i ] = m1 + ( m2 - m1 ) * 6.0f * tempRGB[ i ];
       
   330                 }
       
   331             // HOW TO RETURN hue.to.rgb(m1, m2, h):
       
   332             // IF h*2<1: RETURN m2
       
   333             else if ( 2.0f * tempRGB[ i ] < 1.0f )
       
   334                 {
       
   335                 color[ i ] = m2;
       
   336                 }
       
   337             // HOW TO RETURN hue.to.rgb(m1, m2, h):
       
   338             // IF h*3<2: RETURN m1+(m2-m1)*(2/3-h)*6
       
   339             else if ( 3.0f * tempRGB[ i ] < 2.0f )
       
   340                 {
       
   341                 color[ i ] = m1 + ( m2 - m1 )
       
   342                         * ( ( 2.0f / 3.0f ) - tempRGB[ i ] ) * 6.0f;
       
   343                 }
       
   344             // HOW TO RETURN hue.to.rgb(m1, m2, h):
       
   345             // ELSE RETURN m1
       
   346             else
       
   347                 {
       
   348                 color[ i ] = m1;
       
   349                 }
       
   350             }
       
   351 
       
   352         // Scale color back from [0..1] to [0..255]
       
   353         return new Color( ( int ) ( 255f * color[ 0 ] + 0.5f ),
       
   354                 ( int ) ( 255f * color[ 1 ] + 0.5f ),
       
   355                 ( int ) ( 255f * color[ 2 ] + 0.5f ) );
       
   356         }
       
   357 
       
   358     }