Skip to content

Commit 149fee4

Browse files
committed
[OPT] shortenHEXColors
1 parent 0cf6c08 commit 149fee4

File tree

1 file changed

+58
-34
lines changed

1 file changed

+58
-34
lines changed

src/CSS.php

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public function execute($path = null, $parents = array())
316316
$css = $this->replace($css);
317317

318318
$css = $this->stripWhitespace($css);
319-
$css = $this->shortenColors($css);
319+
$css = $this->shortenHEXColors($css);
320320
$css = $this->shortenZeroes($css);
321321
$css = $this->shortenFontWeights($css);
322322
$css = $this->stripEmptyTags($css);
@@ -480,59 +480,83 @@ protected function move(ConverterInterface $converter, $content)
480480
}
481481

482482
/**
483-
* Shorthand hex color codes.
484-
* #FF0000 -> #F00.
483+
* Shorthand HEX color codes.
484+
* #FF0000 -> #f00 -> red
485485
*
486-
* @param string $content The CSS content to shorten the hex color codes for
486+
* @param string $content The CSS content to shorten the HEX color codes for
487487
*
488488
* @return string
489489
*/
490-
protected function shortenColors($content)
490+
protected function shortenHexColors($content)
491491
{
492-
$content = preg_replace('/(?<=[: ])#([0-9a-z])\\1([0-9a-z])\\2([0-9a-z])\\3(?:([0-9a-z])\\4)?(?=[; }])/i', '#$1$2$3$4', $content);
492+
// shorten repeating patterns within HEX ..
493+
$content = preg_replace('/#([0-9a-f])\\1([0-9a-f])\\2([0-9a-f])\\3(?:([0-9a-f])\\4)?/i', '#$1$2$3$4', $content);
493494

494-
// remove alpha channel if it's pointless...
495-
$content = preg_replace('/(?<=[: ])#([0-9a-z]{6})ff?(?=[; }])/i', '#$1', $content);
496-
$content = preg_replace('/(?<=[: ])#([0-9a-z]{3})f?(?=[; }])/i', '#$1', $content);
495+
// remove alpha channel if it's pointless ..
496+
$content = preg_replace('/#([0-9a-f]{6})ff/i', '#$1', $content);
497+
$content = preg_replace('/#([0-9a-f]{3})f/i', '#$1', $content);
498+
499+
// replace `transparent` with shortcut ..
500+
$content = preg_replace('/#[0-9a-f]{6}00/i', '#0000', $content);
497501

498502
$colors = array(
503+
// make these more readable
504+
'#00f' => 'blue',
505+
'#dc143c' => 'crimson',
506+
'#0ff' => 'cyan',
507+
'#8b0000' => 'darkred',
508+
'#696969' => 'dimgray',
509+
'#ff69b4' => 'hotpink',
510+
'#0f0' => 'lime',
511+
'#fdf5e6' => 'oldlace',
512+
'#87ceeb' => 'skyblue',
513+
'#d8bfd8' => 'thistle',
499514
// we can shorten some even more by replacing them with their color name
500-
'#F0FFFF' => 'azure',
501-
'#F5F5DC' => 'beige',
502-
'#A52A2A' => 'brown',
503-
'#FF7F50' => 'coral',
504-
'#FFD700' => 'gold',
515+
'#f0ffff' => 'azure',
516+
'#f5f5dc' => 'beige',
517+
'#ffe4c4' => 'bisque',
518+
'#a52a2a' => 'brown',
519+
'#ff7f50' => 'coral',
520+
'#ffd700' => 'gold',
505521
'#808080' => 'gray',
506522
'#008000' => 'green',
507-
'#4B0082' => 'indigo',
508-
'#FFFFF0' => 'ivory',
509-
'#F0E68C' => 'khaki',
510-
'#FAF0E6' => 'linen',
523+
'#4b0082' => 'indigo',
524+
'#fffff0' => 'ivory',
525+
'#f0e68c' => 'khaki',
526+
'#faf0e6' => 'linen',
511527
'#800000' => 'maroon',
512528
'#000080' => 'navy',
513529
'#808000' => 'olive',
514-
'#CD853F' => 'peru',
515-
'#FFC0CB' => 'pink',
516-
'#DDA0DD' => 'plum',
530+
'#ffa500' => 'orange',
531+
'#da70d6' => 'orchid',
532+
'#cd853f' => 'peru',
533+
'#ffc0cb' => 'pink',
534+
'#dda0dd' => 'plum',
517535
'#800080' => 'purple',
518-
'#F00' => 'red',
519-
'#FA8072' => 'salmon',
520-
'#A0522D' => 'sienna',
521-
'#C0C0C0' => 'silver',
522-
'#FFFAFA' => 'snow',
523-
'#D2B48C' => 'tan',
524-
'#FF6347' => 'tomato',
525-
'#EE82EE' => 'violet',
526-
'#F5DEB3' => 'wheat',
536+
'#f00' => 'red',
537+
'#fa8072' => 'salmon',
538+
'#a0522d' => 'sienna',
539+
'#c0c0c0' => 'silver',
540+
'#fffafa' => 'snow',
541+
'#d2b48c' => 'tan',
542+
'#008080' => 'teal',
543+
'#ff6347' => 'tomato',
544+
'#ee82ee' => 'violet',
545+
'#f5deb3' => 'wheat',
527546
// or the other way around
528-
'WHITE' => '#fff',
529-
'BLACK' => '#000',
547+
'black' => '#000',
548+
'fuchsia' => '#f0f',
549+
'magenta' => '#f0f',
550+
'white' => '#fff',
551+
'yellow' => '#ff0',
552+
// and also `transparent`
553+
#'transparent' => '#0000' CHECK safari
530554
);
531555

532556
return preg_replace_callback(
533-
'/(?<=[: ])('.implode('|', array_keys($colors)).')(?=[; }])/i',
557+
'/('.implode('|', array_keys($colors)).')/i',
534558
function ($match) use ($colors) {
535-
return $colors[strtoupper($match[0])];
559+
return $colors[strtolower($match[0])];
536560
},
537561
$content
538562
);

0 commit comments

Comments
 (0)