Skip to content
This repository
Browse code

Store color keyword mapping in more compressed format; saves about 30…

…0 bytes
  • Loading branch information...
commit 554ffbd22539f164a9966fa014c1ab3bffc11681 1 parent 9d84290
Jason Johnston authored May 25, 2012
63  sources/Color.js
@@ -56,55 +56,18 @@ PIE.Color = (function() {
56 56
     Color.rgbOrRgbaRE = /\s*rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(,\s*(\d+|\d*\.\d+))?\s*\)\s*/;
57 57
     Color.hslOrHslaRE = /\s*hsla?\(\s*(\d*\.?\d+)\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*(,\s*(\d+|\d*\.\d+))?\s*\)\s*/;
58 58
 
59  
-    Color.names = {
60  
-        "aliceblue":"F0F8FF", "antiquewhite":"FAEBD7", "aqua":"0FF",
61  
-        "aquamarine":"7FFFD4", "azure":"F0FFFF", "beige":"F5F5DC",
62  
-        "bisque":"FFE4C4", "black":"000", "blanchedalmond":"FFEBCD",
63  
-        "blue":"00F", "blueviolet":"8A2BE2", "brown":"A52A2A",
64  
-        "burlywood":"DEB887", "cadetblue":"5F9EA0", "chartreuse":"7FFF00",
65  
-        "chocolate":"D2691E", "coral":"FF7F50", "cornflowerblue":"6495ED",
66  
-        "cornsilk":"FFF8DC", "crimson":"DC143C", "cyan":"0FF",
67  
-        "darkblue":"00008B", "darkcyan":"008B8B", "darkgoldenrod":"B8860B",
68  
-        "darkgray":"A9A9A9", "darkgreen":"006400", "darkkhaki":"BDB76B",
69  
-        "darkmagenta":"8B008B", "darkolivegreen":"556B2F", "darkorange":"FF8C00",
70  
-        "darkorchid":"9932CC", "darkred":"8B0000", "darksalmon":"E9967A",
71  
-        "darkseagreen":"8FBC8F", "darkslateblue":"483D8B", "darkslategray":"2F4F4F",
72  
-        "darkturquoise":"00CED1", "darkviolet":"9400D3", "deeppink":"FF1493",
73  
-        "deepskyblue":"00BFFF", "dimgray":"696969", "dodgerblue":"1E90FF",
74  
-        "firebrick":"B22222", "floralwhite":"FFFAF0", "forestgreen":"228B22",
75  
-        "fuchsia":"F0F", "gainsboro":"DCDCDC", "ghostwhite":"F8F8FF",
76  
-        "gold":"FFD700", "goldenrod":"DAA520", "gray":"808080",
77  
-        "green":"008000", "greenyellow":"ADFF2F", "honeydew":"F0FFF0",
78  
-        "hotpink":"FF69B4", "indianred":"CD5C5C", "indigo":"4B0082",
79  
-        "ivory":"FFFFF0", "khaki":"F0E68C", "lavender":"E6E6FA",
80  
-        "lavenderblush":"FFF0F5", "lawngreen":"7CFC00", "lemonchiffon":"FFFACD",
81  
-        "lightblue":"ADD8E6", "lightcoral":"F08080", "lightcyan":"E0FFFF",
82  
-        "lightgoldenrodyellow":"FAFAD2", "lightgreen":"90EE90", "lightgrey":"D3D3D3",
83  
-        "lightpink":"FFB6C1", "lightsalmon":"FFA07A", "lightseagreen":"20B2AA",
84  
-        "lightskyblue":"87CEFA", "lightslategray":"789", "lightsteelblue":"B0C4DE",
85  
-        "lightyellow":"FFFFE0", "lime":"0F0", "limegreen":"32CD32",
86  
-        "linen":"FAF0E6", "magenta":"F0F", "maroon":"800000",
87  
-        "mediumauqamarine":"66CDAA", "mediumblue":"0000CD", "mediumorchid":"BA55D3",
88  
-        "mediumpurple":"9370D8", "mediumseagreen":"3CB371", "mediumslateblue":"7B68EE",
89  
-        "mediumspringgreen":"00FA9A", "mediumturquoise":"48D1CC", "mediumvioletred":"C71585",
90  
-        "midnightblue":"191970", "mintcream":"F5FFFA", "mistyrose":"FFE4E1",
91  
-        "moccasin":"FFE4B5", "navajowhite":"FFDEAD", "navy":"000080",
92  
-        "oldlace":"FDF5E6", "olive":"808000", "olivedrab":"688E23",
93  
-        "orange":"FFA500", "orangered":"FF4500", "orchid":"DA70D6",
94  
-        "palegoldenrod":"EEE8AA", "palegreen":"98FB98", "paleturquoise":"AFEEEE",
95  
-        "palevioletred":"D87093", "papayawhip":"FFEFD5", "peachpuff":"FFDAB9",
96  
-        "peru":"CD853F", "pink":"FFC0CB", "plum":"DDA0DD",
97  
-        "powderblue":"B0E0E6", "purple":"800080", "red":"F00",
98  
-        "rosybrown":"BC8F8F", "royalblue":"4169E1", "saddlebrown":"8B4513",
99  
-        "salmon":"FA8072", "sandybrown":"F4A460", "seagreen":"2E8B57",
100  
-        "seashell":"FFF5EE", "sienna":"A0522D", "silver":"C0C0C0",
101  
-        "skyblue":"87CEEB", "slateblue":"6A5ACD", "slategray":"708090",
102  
-        "snow":"FFFAFA", "springgreen":"00FF7F", "steelblue":"4682B4",
103  
-        "tan":"D2B48C", "teal":"008080", "thistle":"D8BFD8",
104  
-        "tomato":"FF6347", "turquoise":"40E0D0", "violet":"EE82EE",
105  
-        "wheat":"F5DEB3", "white":"FFF", "whitesmoke":"F5F5F5",
106  
-        "yellow":"FF0", "yellowgreen":"9ACD32"
107  
-    };
  59
+    /**
  60
+     * Hash of color keyword names to their corresponding hex values. Encoded for
  61
+     * small size and expanded into a hash on startup.
  62
+     */
  63
+    Color.names = {};
  64
+
  65
+    var names = 'aliceblue|9ehhb|antiquewhite|9sgk7|aqua|1ekf|aquamarine|4zsno|azure|9eiv3|beige|9lhp8|bisque|9zg04|black|0|blanchedalmond|9zhe5|blue|73|blueviolet|5e31e|brown|6g016|burlywood|8ouiv|cadetblue|3qba8|chartreuse|4zshs|chocolate|87k0u|coral|9yvyo|cornflowerblue|3xael|cornsilk|9zjz0|crimson|8l4xo|cyan|1ekf|darkblue|3v|darkcyan|rkb|darkgoldenrod|776yz|darkgray|6mbhl|darkgreen|jr4|darkkhaki|7ehkb|darkmagenta|5f91n|darkolivegreen|3bzfz|darkorange|9yygw|darkorchid|5z6x8|darkred|5f8xs|darksalmon|9441m|darkseagreen|5lwgf|darkslateblue|2th1n|darkslategray|1ugcv|darkturquoise|14up|darkviolet|5rw7n|deeppink|9yavn|deepskyblue|11xb|dimgray|442g9|dodgerblue|16xof|firebrick|6y7tu|floralwhite|9zkds|forestgreen|1cisi|fuchsia|9y70f|gainsboro|8m8kc|ghostwhite|9pq0v|gold|9zda8|goldenrod|8j4f4|gray|50i2o|green|pa8|greenyellow|6senj|honeydew|9eiuo|hotpink|9yrp0|indianred|80gnw|indigo|2xcoy|ivory|9zldc|khaki|9edu4|lavender|90c8q|lavenderblush|9ziet|lawngreen|4vk74|lemonchiffon|9zkct|lightblue|6s73a|lightcoral|9dtog|lightcyan|8s1rz|lightgoldenrodyellow|9sjiq|lightgreen|5nkwg|lightgrey|89jo3|lightpink|9z6wx|lightsalmon|9z2ii|lightseagreen|19xgq|lightskyblue|5arju|lightslategray|4nwk9|lightsteelblue|6wau6|lightyellow|9zlcw|lime|1edc|limegreen|1zcxe|linen|9shk6|magenta|9y70f|maroon|4zsow|mediumauqamarine|40eju|mediumblue|5p|mediumorchid|79qkz|mediumpurple|5r3rs|mediumseagreen|2d9ip|mediumslateblue|4tcku|mediumspringgreen|1di2|mediumturquoise|2uabw|mediumvioletred|7rn9h|midnightblue|z980|mintcream|9ljp6|mistyrose|9zg0x|moccasin|9zfzp|navajowhite|9zest|navy|3k|oldlace|9wq92|olive|50hz4|olivedrab|42v4z|orange|9z3eo|orangered|9ykg0|orchid|8iu3a|palegoldenrod|9bl4a|palegreen|5yw0o|paleturquoise|6v4ku|palevioletred|8g0wj|papayawhip|9zi6t|peachpuff|9ze0p|peru|80oqn|pink|9z8wb|plum|8nba5|powderblue|6wgdi|purple|4zssg|red|9y6tc|rosybrown|7cv4f|royalblue|2jvtt|saddlebrown|5fmkz|salmon|9rvci|sandybrown|9jn1c|seagreen|1tdnb|seashell|9zje6|sienna|6973h|silver|7ir40|skyblue|5arjf|slateblue|45e4t|slategray|4e100|snow|9zke2|springgreen|1egv|steelblue|2r1kk|tan|87yx8|teal|pds|thistle|8ggk8|tomato|9yqfb|turquoise|2j4r4|violet|9b10u|wheat|9ld4j|white|9zldr|whitesmoke|9lhpx|yellow|9zl6o|yellowgreen|61fzm'.split('|'),
  66
+        i = 0, len = names.length, val;
  67
+    for(; i < len; i += 2) {
  68
+        val = parseInt(names[i + 1], 36).toString(16);
  69
+        Color.names[names[i]] = '#' + ('000000' + val).substr(val.length);
  70
+    }
108 71
 
109 72
     Color.prototype = {
110 73
         /**
@@ -129,7 +92,7 @@ PIE.Color = (function() {
129 92
                 }
130 93
                 else {
131 94
                     if( Color.names.hasOwnProperty( vLower = color.toLowerCase() ) ) {
132  
-                        color = '#' + Color.names[vLower];
  95
+                        color = Color.names[vLower];
133 96
                     }
134 97
                     alpha = ( color === 'transparent' ? 0 : 1 );
135 98
                 }
8  tests/color-names.html
@@ -30,7 +30,7 @@
30 30
     <script type="text/javascript">
31 31
         // Some simple unit tests of color parsing
32 32
         var colorValueTests = {
33  
-                'aqua': '#0FF',
  33
+                'aqua': '#00FFFF',
34 34
                 'beige': '#F5F5DC',
35 35
                 '#123456': '#123456',
36 36
                 '#123': '#123',
@@ -40,7 +40,7 @@
40 40
                 'hsl(0, 100%, 50%)': '#ff0000',
41 41
                 'hsla(0, 100%, 50%, 0.5)': '#ff0000',
42 42
                 'hsla(0, 100%, 50%, .5)': '#ff0000',
43  
-                'currentColor': '#F00',
  43
+                'currentColor': '#FF0000',
44 44
                 'transparent': 'transparent'
45 45
             },
46 46
             hexValueTests = {
@@ -77,14 +77,14 @@
77 77
             c, val;
78 78
         for (c in colorValueTests) {
79 79
             val = PIE.getColor( c ).colorValue( currentColorTester );
80  
-            if ( val !== colorValueTests[ c ] ) {
  80
+            if ( val.toLowerCase() !== colorValueTests[ c ].toLowerCase() ) {
81 81
                 document.write('<p style="color:red">PIE.Color#colorValue() failed for "' + c + '": got "' + val + '", expected "' + colorValueTests[c] + '"</p>')
82 82
                 failed = true;
83 83
             }
84 84
         }
85 85
         for (c in hexValueTests) {
86 86
             val = PIE.getColor( c ).hexValue( currentColorTester );
87  
-            if ( val !== hexValueTests[ c ] ) {
  87
+            if ( val.toLowerCase() !== hexValueTests[ c ].toLowerCase() ) {
88 88
                 document.write('<p style="color:red">PIE.Color#hexValue() failed for "' + c + '": got "' + val + '", expected "' + hexValueTests[c] + '"</p>')
89 89
                 failed = true;
90 90
             }

0 notes on commit 554ffbd

Please sign in to comment.
Something went wrong with that request. Please try again.