Skip to content

Commit

Permalink
Shorten colors some more if their color name is shorter
Browse files Browse the repository at this point in the history
As suggested on issue #109
  • Loading branch information
matthiasmullie committed Jun 9, 2016
1 parent 1a15254 commit 3253a81
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,38 @@ protected function shortenHex($content)
{
$content = preg_replace('/(?<![\'"])#([0-9a-z])\\1([0-9a-z])\\2([0-9a-z])\\3(?![\'"])/i', '#$1$2$3', $content);

return $content;
// we can shorten some even more by replacing them with their color name
$colors = array(
'#F0FFFF' => 'azure',
'#F5F5DC' => 'beige',
'#A52A2A' => 'brown',
'#FF7F50' => 'coral',
'#FFD700' => 'gold',
'#808080' => 'gray',
'#008000' => 'green',
'#4B0082' => 'indigo',
'#FFFFF0' => 'ivory',
'#F0E68C' => 'khaki',
'#FAF0E6' => 'linen',
'#800000' => 'maroon',
'#000080' => 'navy',
'#808000' => 'olive',
'#CD853F' => 'peru',
'#FFC0CB' => 'pink',
'#DDA0DD' => 'plum',
'#800080' => 'purple',
'#F00' => 'red',

This comment has been minimized.

Copy link
@artefactio

artefactio Sep 29, 2016

I believe this line is wrong, for example border: 1px solid #f00000; is becoming border: 1px solid red000;

This comment has been minimized.

Copy link
@matthiasmullie

matthiasmullie Nov 23, 2016

Author Owner

Hey @artefactio

I lost track of this comment. Luckily happened to stumble onto it again.
This is now fixed in 6340735
Thanks!

'#FA8072' => 'salmon',
'#A0522D' => 'sienna',
'#C0C0C0' => 'silver',
'#FFFAFA' => 'snow',
'#D2B48C' => 'tan',
'#FF6347' => 'tomato',
'#EE82EE' => 'violet',
'#F5DEB3' => 'wheat',
);

return str_ireplace(array_keys($colors), $colors, $content);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/css/CSSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ public function dataProvider()
}',
'p{font-weight:400}',
);
$tests[] = array(
'p{background-color:#ff0000}',
'p{background-color:red}',
);
$tests[] = array(
'p{color:#F0E68C}',
'p{color:khaki}',
);

return $tests;
}
Expand Down

0 comments on commit 3253a81

Please sign in to comment.