Skip to content
This repository
Browse code

Do not compress IE ARGB colors.

commit 52ba9a4ccf4e9a895d557f01112ab4c7f4cad8a7 1 parent b1115b9
Adrian Phinney authored August 22, 2012 lukeapage committed September 16, 2012
6  src/dotless.Core/Parser/Parsers.cs
@@ -374,13 +374,13 @@ public Variable Variable(Parser parser)
374 374
         //
375 375
         public Color Color(Parser parser)
376 376
         {
377  
-            RegexMatchResult rgb;
  377
+            RegexMatchResult hex;
378 378
 
379 379
             var index = parser.Tokenizer.Location.Index;
380 380
 
381 381
             if (parser.Tokenizer.CurrentChar == '#' &&
382  
-                (rgb = parser.Tokenizer.Match(@"#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})")))
383  
-                return NodeProvider.Color(rgb[1], parser.Tokenizer.GetNodeLocation(index));
  382
+                (hex = parser.Tokenizer.Match(@"#([a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})")))
  383
+                return NodeProvider.Color(hex[1], parser.Tokenizer.GetNodeLocation(index));
384 384
 
385 385
             return null;
386 386
         }
28  src/dotless.Core/Parser/Tree/Color.cs
@@ -184,6 +184,8 @@ static Color()
184 184
             }
185 185
         }
186 186
 
  187
+        private bool isArgb = false;
  188
+
187 189
         public readonly double[] RGB;
188 190
         public readonly double Alpha;
189 191
 
@@ -206,22 +208,32 @@ public Color(IEnumerable<Number> rgb, Number alpha)
206 208
             Alpha = alpha.Normalize();
207 209
         }
208 210
 
209  
-        public Color(string rgb)
  211
+        public Color(string hex)
210 212
         {
211  
-            if (rgb.Length == 6)
  213
+            Alpha = 1;
  214
+
  215
+            if (hex.Length == 8)
  216
+            {
  217
+                isArgb = true;
  218
+                RGB = Enumerable.Range(1, 3)
  219
+                    .Select(i => hex.Substring(i * 2, 2))
  220
+                    .Select(s => (double) int.Parse(s, NumberStyles.HexNumber))
  221
+                    .ToArray();
  222
+                Alpha = (double) int.Parse(hex.Substring(0, 2), NumberStyles.HexNumber) / 255d;
  223
+            }
  224
+            else if (hex.Length == 6)
212 225
             {
213 226
                 RGB = Enumerable.Range(0, 3)
214  
-                    .Select(i => rgb.Substring(i*2, 2))
  227
+                    .Select(i => hex.Substring(i*2, 2))
215 228
                     .Select(s => (double) int.Parse(s, NumberStyles.HexNumber))
216 229
                     .ToArray();
217 230
             }
218 231
             else
219 232
             {
220  
-                RGB = rgb.ToCharArray()
  233
+                RGB = hex.ToCharArray()
221 234
                     .Select(c => (double) int.Parse("" + c + c, NumberStyles.HexNumber))
222 235
                     .ToArray();
223 236
             }
224  
-            Alpha = 1;
225 237
         }
226 238
 
227 239
         public Color(double red, double green, double blue, double alpha)
@@ -284,6 +296,12 @@ public override void AppendCSS(Env env)
284 296
                 return;
285 297
             }
286 298
 
  299
+            if (isArgb)
  300
+            {
  301
+                env.Output.Append(ToArgb());
  302
+                return;
  303
+            }
  304
+
287 305
             if (Alpha < 1.0)
288 306
             {
289 307
                 env.Output.AppendFormat(CultureInfo.InvariantCulture, "rgba({0}, {1}, {2}, {3})", rgb[0], rgb[1], rgb[2], Alpha);
7  src/dotless.Test/Specs/Compression/ColorsFixture.cs
@@ -10,6 +10,13 @@ public void Colors()
10 10
             AssertExpression("#fea", "#ffeeaa");
11 11
             AssertExpression("blue", "#0000ff");
12 12
         }
  13
+
  14
+        [Test]
  15
+        public void Should_not_compress_IE_ARGB()
  16
+        {
  17
+            AssertExpressionUnchanged("#ffaabbcc");
  18
+            AssertExpressionUnchanged("#aabbccdd");
  19
+        }
13 20
         
14 21
         [Test]
15 22
         public void Overflow()

0 notes on commit 52ba9a4

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