CSSやHTMLで指定される色の名前から16進数表記を取得する

CSSやHTMLで指定される色の名前から16進数表記を取得したくなった。

例えば

<p style=”color: green;”>
の用に書かれているgreenはRGBの16進表記では008000で、次の

<p style=”color: #008000;”>

と同じ意味だ。このとき、greenという色の名前から008000が欲しくなった。一覧はググルとでてきた。

CSS Color Names

http://www.w3schools.com/cssref/css_colornames.asp

そんでこれを変換するPHPでコードを書いてみた。

<?php

main();

function main() {
 global $palettes;
 var_dump(getColorHexByName('green'));
}

function getColorHexByName($colorName) {
 $palettes = array(
 array('name' => 'AliceBlue', 'hex' => 'F0F8FF'),
 array('name' => 'AntiqueWhite', 'hex' => 'FAEBD7'),
 array('name' => 'Aqua', 'hex' => '00FFFF'),
 array('name' => 'Aquamarine', 'hex' => '7FFFD4'),
 array('name' => 'Azure', 'hex' => 'F0FFFF'),
 array('name' => 'Beige', 'hex' => 'F5F5DC'),
 array('name' => 'Bisque', 'hex' => 'FFE4C4'),
 array('name' => 'Black', 'hex' => '000000'),
 array('name' => 'BlanchedAlmond', 'hex' => 'FFEBCD'),
 array('name' => 'Blue', 'hex' => '0000FF'),
 array('name' => 'BlueViolet', 'hex' => '8A2BE2'),
 array('name' => 'Brown', 'hex' => 'A52A2A'),
 array('name' => 'BurlyWood', 'hex' => 'DEB887'),
 array('name' => 'CadetBlue', 'hex' => '5F9EA0'),
 array('name' => 'Chartreuse', 'hex' => '7FFF00'),
 array('name' => 'Chocolate', 'hex' => 'D2691E'),
 array('name' => 'Coral', 'hex' => 'FF7F50'),
 array('name' => 'CornflowerBlue', 'hex' => '6495ED'),
 array('name' => 'Cornsilk', 'hex' => 'FFF8DC'),
 array('name' => 'Crimson', 'hex' => 'DC143C'),
 array('name' => 'Cyan', 'hex' => '00FFFF'),
 array('name' => 'DarkBlue', 'hex' => '00008B'),
 array('name' => 'DarkCyan', 'hex' => '008B8B'),
 array('name' => 'DarkGoldenRod', 'hex' => 'B8860B'),
 array('name' => 'DarkGray', 'hex' => 'A9A9A9'),
 array('name' => 'DarkGreen', 'hex' => '006400'),
 array('name' => 'DarkKhaki', 'hex' => 'BDB76B'),
 array('name' => 'DarkMagenta', 'hex' => '8B008B'),
 array('name' => 'DarkOliveGreen', 'hex' => '556B2F'),
 array('name' => 'DarkOrange', 'hex' => 'FF8C00'),
 array('name' => 'DarkOrchid', 'hex' => '9932CC'),
 array('name' => 'DarkRed', 'hex' => '8B0000'),
 array('name' => 'DarkSalmon', 'hex' => 'E9967A'),
 array('name' => 'DarkSeaGreen', 'hex' => '8FBC8F'),
 array('name' => 'DarkSlateBlue', 'hex' => '483D8B'),
 array('name' => 'DarkSlateGray', 'hex' => '2F4F4F'),
 array('name' => 'DarkTurquoise', 'hex' => '00CED1'),
 array('name' => 'DarkViolet', 'hex' => '9400D3'),
 array('name' => 'DeepPink', 'hex' => 'FF1493'),
 array('name' => 'DeepSkyBlue', 'hex' => '00BFFF'),
 array('name' => 'DimGray', 'hex' => '696969'),
 array('name' => 'DodgerBlue', 'hex' => '1E90FF'),
 array('name' => 'FireBrick', 'hex' => 'B22222'),
 array('name' => 'FloralWhite', 'hex' => 'FFFAF0'),
 array('name' => 'ForestGreen', 'hex' => '228B22'),
 array('name' => 'Fuchsia', 'hex' => 'FF00FF'),
 array('name' => 'Gainsboro', 'hex' => 'DCDCDC'),
 array('name' => 'GhostWhite', 'hex' => 'F8F8FF'),
 array('name' => 'Gold', 'hex' => 'FFD700'),
 array('name' => 'GoldenRod', 'hex' => 'DAA520'),
 array('name' => 'Gray', 'hex' => '808080'),
 array('name' => 'Green', 'hex' => '008000'),
 array('name' => 'GreenYellow', 'hex' => 'ADFF2F'),
 array('name' => 'HoneyDew', 'hex' => 'F0FFF0'),
 array('name' => 'HotPink', 'hex' => 'FF69B4'),
 array('name' => 'IndianRed ', 'hex' => 'CD5C5C'),
 array('name' => 'Indigo ', 'hex' => '4B0082'),
 array('name' => 'Ivory', 'hex' => 'FFFFF0'),
 array('name' => 'Khaki', 'hex' => 'F0E68C'),
 array('name' => 'Lavender', 'hex' => 'E6E6FA'),
 array('name' => 'LavenderBlush', 'hex' => 'FFF0F5'),
 array('name' => 'LawnGreen', 'hex' => '7CFC00'),
 array('name' => 'LemonChiffon', 'hex' => 'FFFACD'),
 array('name' => 'LightBlue', 'hex' => 'ADD8E6'),
 array('name' => 'LightCoral', 'hex' => 'F08080'),
 array('name' => 'LightCyan', 'hex' => 'E0FFFF'),
 array('name' => 'LightGoldenRodYellow', 'hex' => 'FAFAD2'),
 array('name' => 'LightGray', 'hex' => 'D3D3D3'),
 array('name' => 'LightGreen', 'hex' => '90EE90'),
 array('name' => 'LightPink', 'hex' => 'FFB6C1'),
 array('name' => 'LightSalmon', 'hex' => 'FFA07A'),
 array('name' => 'LightSeaGreen', 'hex' => '20B2AA'),
 array('name' => 'LightSkyBlue', 'hex' => '87CEFA'),
 array('name' => 'LightSlateGray', 'hex' => '778899'),
 array('name' => 'LightSteelBlue', 'hex' => 'B0C4DE'),
 array('name' => 'LightYellow', 'hex' => 'FFFFE0'),
 array('name' => 'Lime', 'hex' => '00FF00'),
 array('name' => 'LimeGreen', 'hex' => '32CD32'),
 array('name' => 'Linen', 'hex' => 'FAF0E6'),
 array('name' => 'Magenta', 'hex' => 'FF00FF'),
 array('name' => 'Maroon', 'hex' => '800000'),
 array('name' => 'MediumAquaMarine', 'hex' => '66CDAA'),
 array('name' => 'MediumBlue', 'hex' => '0000CD'),
 array('name' => 'MediumOrchid', 'hex' => 'BA55D3'),
 array('name' => 'MediumPurple', 'hex' => '9370DB'),
 array('name' => 'MediumSeaGreen', 'hex' => '3CB371'),
 array('name' => 'MediumSlateBlue', 'hex' => '7B68EE'),
 array('name' => 'MediumSpringGreen', 'hex' => '00FA9A'),
 array('name' => 'MediumTurquoise', 'hex' => '48D1CC'),
 array('name' => 'MediumVioletRed', 'hex' => 'C71585'),
 array('name' => 'MidnightBlue', 'hex' => '191970'),
 array('name' => 'MintCream', 'hex' => 'F5FFFA'),
 array('name' => 'MistyRose', 'hex' => 'FFE4E1'),
 array('name' => 'Moccasin', 'hex' => 'FFE4B5'),
 array('name' => 'NavajoWhite', 'hex' => 'FFDEAD'),
 array('name' => 'Navy', 'hex' => '000080'),
 array('name' => 'OldLace', 'hex' => 'FDF5E6'),
 array('name' => 'Olive', 'hex' => '808000'),
 array('name' => 'OliveDrab', 'hex' => '6B8E23'),
 array('name' => 'Orange', 'hex' => 'FFA500'),
 array('name' => 'OrangeRed', 'hex' => 'FF4500'),
 array('name' => 'Orchid', 'hex' => 'DA70D6'),
 array('name' => 'PaleGoldenRod', 'hex' => 'EEE8AA'),
 array('name' => 'PaleGreen', 'hex' => '98FB98'),
 array('name' => 'PaleTurquoise', 'hex' => 'AFEEEE'),
 array('name' => 'PaleVioletRed', 'hex' => 'DB7093'),
 array('name' => 'PapayaWhip', 'hex' => 'FFEFD5'),
 array('name' => 'PeachPuff', 'hex' => 'FFDAB9'),
 array('name' => 'Peru', 'hex' => 'CD853F'),
 array('name' => 'Pink', 'hex' => 'FFC0CB'),
 array('name' => 'Plum', 'hex' => 'DDA0DD'),
 array('name' => 'PowderBlue', 'hex' => 'B0E0E6'),
 array('name' => 'Purple', 'hex' => '800080'),
 array('name' => 'Red', 'hex' => 'FF0000'),
 array('name' => 'RosyBrown', 'hex' => 'BC8F8F'),
 array('name' => 'RoyalBlue', 'hex' => '4169E1'),
 array('name' => 'SaddleBrown', 'hex' => '8B4513'),
 array('name' => 'Salmon', 'hex' => 'FA8072'),
 array('name' => 'SandyBrown', 'hex' => 'F4A460'),
 array('name' => 'SeaGreen', 'hex' => '2E8B57'),
 array('name' => 'SeaShell', 'hex' => 'FFF5EE'),
 array('name' => 'Sienna', 'hex' => 'A0522D'),
 array('name' => 'Silver', 'hex' => 'C0C0C0'),
 array('name' => 'SkyBlue', 'hex' => '87CEEB'),
 array('name' => 'SlateBlue', 'hex' => '6A5ACD'),
 array('name' => 'SlateGray', 'hex' => '708090'),
 array('name' => 'Snow', 'hex' => 'FFFAFA'),
 array('name' => 'SpringGreen', 'hex' => '00FF7F'),
 array('name' => 'SteelBlue', 'hex' => '4682B4'),
 array('name' => 'Tan', 'hex' => 'D2B48C'),
 array('name' => 'Teal', 'hex' => '008080'),
 array('name' => 'Thistle', 'hex' => 'D8BFD8'),
 array('name' => 'Tomato', 'hex' => 'FF6347'),
 array('name' => 'Turquoise', 'hex' => '40E0D0'),
 array('name' => 'Violet', 'hex' => 'EE82EE'),
 array('name' => 'Wheat', 'hex' => 'F5DEB3'),
 array('name' => 'White', 'hex' => 'FFFFFF'),
 array('name' => 'WhiteSmoke', 'hex' => 'F5F5F5'),
 array('name' => 'Yellow', 'hex' => 'FFFF00'),
 array('name' => 'YellowGreen', 'hex' => '9ACD32'),
 );

foreach($palettes as $palette) {
   if (strtolower($palette['name']) == strtolower($colorName)) {
   return $palette['hex'];
   }
 }
 return false;
}
?>

これでgetColorHexByName(‘green’) のように色の名前を入れると0800000みたいな16進でとれる!! 便利!きっと便利!

コメントを残す