CSS compression: reducing colour values

assorted-color smoke

In the upcoming month I’m going to share a number of ways of compressing CSS. And one of the most basic is to reduce the various colour values down.

Usually, hex values are used and, sometimes, the colour names. And choosing the right one can save up to 13 characters per reference. Let’s explore how…

First of all, I’m going to assume here that any hex values have been reduced down – so, for example, #111111 would now be #111 and #AABBCC would be #ABC. There are 4096 examples of where this can be applied (out of the 16m+ ranges that are available).

Hex to Names

Hex ValueColour NameCharacter Reduction
#000080Navy3
#008000Green2
#008080Teal3
#00FF00Lime3
#4B0082Indigo1
#800000Maroon1
#800080Purple1
#808000Olive2
#808080Gray3
#A0522DSienna1
#A52A2ABrown2
#C0C0C0Silver1
#CD853FPeru3
#D2B48CTan4
#DA70D6Orchid1
#DDA0DDPlum3
#EE82EEViolet1
#F0E68CKhaki2
#F00Red1
#F0FFFFAzure2
#F5DEB3Wheat2
#F5F5DCBeige2
#FA8072Salmon1
#FAF0E6Linen2
#FF6347Tomato1
#FF7F50Coral2
#FFA500Orange1
#FFC0CBPink3
#FFD700Gold3
#FFE4C4Bisque1
#FFFAFASnow3
#FFFFF0Ivory2
#808080Grey3

Here’s an array that can be used in your code of the above values…

$tocolor = array(
'#000080' => 'Navy',
'#008000' => 'Green',
'#008080' => 'Teal',
'#00FF00' => 'Lime',
'#4B0082' => 'Indigo',
'#800000' => 'Maroon',
'#800080' => 'Purple',
'#808000' => 'Olive',
'#808080' => 'Gray',
'#A0522D' => 'Sienna',
'#A52A2A' => 'Brown',
'#C0C0C0' => 'Silver',
'#CD853F' => 'Peru',
'#D2B48C' => 'Tan',
'#DA70D6' => 'Orchid',
'#DDA0DD' => 'Plum',
'#EE82EE' => 'Violet',
'#F0E68C' => 'Khaki',
'#F00' => 'Red',
'#F0FFFF' => 'Azure',
'#F5DEB3' => 'Wheat',
'#F5F5DC' => 'Beige',
'#FA8072' => 'Salmon',
'#FAF0E6' => 'Linen',
'#FF6347' => 'Tomato',
'#FF7F50' => 'Coral',
'#FFA500' => 'Orange',
'#FFC0CB' => 'Pink',
'#FFD700' => 'Gold',
'#FFE4C4' => 'Bisque',
'#FFFAFA' => 'Snow',
'#FFFFF0' => 'Ivory',
'#808080' => 'Grey',
);
view raw hextocolor.php hosted with ❤ by GitHub

Names to Hex

Colour NameHex ValueCharacter Reduction
Black#0001
DarkBlue#00008B1
MediumBlue#0000CD3
DarkGreen#0064002
DarkCyan#008B8B1
DeepSkyBlue#00BFFF4
DarkTurquoise#00CED16
MediumSpringGreen#00FA9A10
SpringGreen#00FF7F4
MidnightBlue#1919705
DodgerBlue#1E90FF3
LightSeaGreen#20B2AA6
ForestGreen#228B224
SeaGreen#2E8B571
DarkSlateGray#2F4F4F6
LimeGreen#32CD322
MediumSeaGreen#3CB3717
Turquoise#40E0D02
RoyalBlue#4169E12
SteelBlue#4682B42
DarkSlateBlue#483D8B6
MediumTurquoise#48D1CC8
DarkOliveGreen#556B2F7
CadetBlue#5F9EA02
CornFlowerBlue#6495ED7
MediumAquaMarine#66CDAA9
SlateBlue#6A5ACD2
OliveDrab#6B8E232
SlateGray#7080902
LightSlateGray#7788997
MediumSlateBlue#7B68EE8
LawnGreen#7CFC002
Chartreuse#7FFF003
AquaMarine#7FFFD43
LightSkyBlue#87CEFA5
BlueViolet#8A2BE23
DarkMagenta#8B008B4
SaddleBrown#8B45134
DarkSeaGreen#8FBC8F5
LightGreen#90EE903
MediumPurple#9370DB5
DarkViolet#9400D33
PaleGreen#98FB982
DarkOrchid#9932CC3
YellowGreen#9ACD324
DarkGray#A9A9A91
DarkGrey#A9A9A91
LightBlue#ADD8E62
GreenYellow#ADFF2F4
PaleTurquoise#AFEEEE6
LightSteelBlue#B0C4DE7
PowderBlue#B0E0E63
Firebrick#B222222
DarkGoldenRod#B8860B6
MediumOrchid#BA55D35
RosyBrown#BC8F8F2
DarkKhaki#BDB76B2
MediumVioletRed#C715858
IndianRed#CD5C5C2
Chocolate#D2691E2
LightGray#D3D3D32
LightGrey#D3D3D32
GoldenRod#DAA5202
PaleVioletRed#DB70936
Gainsboro#DCDCDC2
BurlyWood#DEB8872
LightCyan#E0FFFF2
Lavender#E6E6FA1
DarkSalmon#E9967A3
PaleGoldenRod#EEE8AA6
LightCoral#F080803
AliceBlue#F0F8FF2
Honeydew#F0FFF01
SandyBrown#F4A4603
WhiteSmoke#F5F5F53
MintCream#F5FFFA2
GhostWhite#F8F8FF3
AntiqueWhite#FAEBD75
LightGoldenRodYellow#FAFAD213
Magenta#F0F3
Fuchsia#F0F3
DeepPink#FF14931
OrangeRed#FF45002
DarkOrange#FF8C003
LightSalmon#FFA07A4
LightPink#FFB6C12
PeachPuff#FFDAB92
NavajoWhite#FFDEAD4
Moccasin#FFE4B51
MistyRose#FFE4E12
BlanchedAlmond#FFEBCD7
PapayaWhip#FFEFD53
LavenderBlush#FFF0F56
SeaShell#FFF5EE1
CornSilk#FFF8DC1
LemonChiffon#FFFACD5
FloralWhite#FFFAF04
Yellow#FF02
LightYellow#FFFFE04
White#FFF1

And here’s an array of this…

$tocolor = array(
'Black' => '#000',
'DarkBlue' => '#00008B',
'MediumBlue' => '#0000CD',
'DarkGreen' => '#006400',
'DarkCyan' => '#008B8B',
'DeepSkyBlue' => '#00BFFF',
'DarkTurquoise' => '#00CED1',
'MediumSpringGreen' => '#00FA9A',
'SpringGreen' => '#00FF7F',
'MidnightBlue' => '#191970',
'DodgerBlue' => '#1E90FF',
'LightSeaGreen' => '#20B2AA',
'ForestGreen' => '#228B22',
'SeaGreen' => '#2E8B57',
'DarkSlateGray' => '#2F4F4F',
'LimeGreen' => '#32CD32',
'MediumSeaGreen' => '#3CB371',
'Turquoise' => '#40E0D0',
'RoyalBlue' => '#4169E1',
'SteelBlue' => '#4682B4',
'DarkSlateBlue' => '#483D8B',
'MediumTurquoise' => '#48D1CC',
'DarkOliveGreen' => '#556B2F',
'CadetBlue' => '#5F9EA0',
'CornFlowerBlue' => '#6495ED',
'MediumAquaMarine' => '#66CDAA',
'SlateBlue' => '#6A5ACD',
'OliveDrab' => '#6B8E23',
'SlateGray' => '#708090',
'LightSlateGray' => '#778899',
'MediumSlateBlue' => '#7B68EE',
'LawnGreen' => '#7CFC00',
'Chartreuse' => '#7FFF00',
'AquaMarine' => '#7FFFD4',
'LightSkyBlue' => '#87CEFA',
'BlueViolet' => '#8A2BE2',
'DarkMagenta' => '#8B008B',
'SaddleBrown' => '#8B4513',
'DarkSeaGreen' => '#8FBC8F',
'LightGreen' => '#90EE90',
'MediumPurple' => '#9370DB',
'DarkViolet' => '#9400D3',
'PaleGreen' => '#98FB98',
'DarkOrchid' => '#9932CC',
'YellowGreen' => '#9ACD32',
'DarkGray' => '#A9A9A9',
'DarkGrey' => '#A9A9A9',
'LightBlue' => '#ADD8E6',
'GreenYellow' => '#ADFF2F',
'PaleTurquoise' => '#AFEEEE',
'LightSteelBlue' => '#B0C4DE',
'PowderBlue' => '#B0E0E6',
'Firebrick' => '#B22222',
'DarkGoldenRod' => '#B8860B',
'MediumOrchid' => '#BA55D3',
'RosyBrown' => '#BC8F8F',
'DarkKhaki' => '#BDB76B',
'MediumVioletRed' => '#C71585',
'IndianRed' => '#CD5C5C',
'Chocolate' => '#D2691E',
'LightGray' => '#D3D3D3',
'LightGrey' => '#D3D3D3',
'GoldenRod' => '#DAA520',
'PaleVioletRed' => '#DB7093',
'Gainsboro' => '#DCDCDC',
'BurlyWood' => '#DEB887',
'LightCyan' => '#E0FFFF',
'Lavender' => '#E6E6FA',
'DarkSalmon' => '#E9967A',
'PaleGoldenRod' => '#EEE8AA',
'LightCoral' => '#F08080',
'AliceBlue' => '#F0F8FF',
'Honeydew' => '#F0FFF0',
'SandyBrown' => '#F4A460',
'WhiteSmoke' => '#F5F5F5',
'MintCream' => '#F5FFFA',
'GhostWhite' => '#F8F8FF',
'AntiqueWhite' => '#FAEBD7',
'LightGoldenRodYellow' => '#FAFAD2',
'Magenta' => '#F0F',
'Fuchsia' => '#F0F',
'DeepPink' => '#FF1493',
'OrangeRed' => '#FF4500',
'DarkOrange' => '#FF8C00',
'LightSalmon' => '#FFA07A',
'LightPink' => '#FFB6C1',
'PeachPuff' => '#FFDAB9',
'NavajoWhite' => '#FFDEAD',
'Moccasin' => '#FFE4B5',
'MistyRose' => '#FFE4E1',
'BlanchedAlmond' => '#FFEBCD',
'PapayaWhip' => '#FFEFD5',
'LavenderBlush' => '#FFF0F5',
'SeaShell' => '#FFF5EE',
'CornSilk' => '#FFF8DC',
'LemonChiffon' => '#FFFACD',
'FloralWhite' => '#FFFAF0',
'Yellow' => '#FF0',
'LightYellow' => '#FFFFE0',
'White' => '#FFF',
);
view raw colortohex.php hosted with ❤ by GitHub

RGB Colours

In CSS you can also specify colours in decimal RGB format, where you use 0-255 for each of the three colours.

For example, where red is #ff0000 in hex, in RGB it’s listed as rgb(255, 0, 0). In all cases, even when you strip out the un-needed spaves, converting away from RGB is better.

My recommendation would be to do this first and convert to hex first before sweeping for any hex to colour name improvement.

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading