Pastey get x11 color names

-- color name map convert
-- ne1 6/16 
-- redylib demo 
-- http://centerkey.com/colors/ sorted x11 color names 
 
 
include std/os.e 
include std/io.e 
include std/sequence.e as seq 
include std/convert.e 
include std/text.e  
include std/map.e  
include std/error.e 
 
 
-- rgb/get_rgb from redylib, not sure from where exactly 
--[01:44] <@stagelark>  
 
public function rgb(atom r, atom g, atom b) 
	return r + g * 256 + b * 65536 
end function 
 
public function get_rgb(atom color) 
	return int_to_bytes(color) 
end function 
 
 
-- return hex string given color name or color name given hex string 
-- obviously,if not found needs to find a match somehow 
-- a nearest color or default color return 
-- crash would be unacceptable 
-- should also lower case the sequence if not a number,  
-- since names are stored lower case and all string 
-- should be able to handle hex as number if name not found too 
 
public function get_colorname_orhex(map colormap, object hexcolor) 
	object hc 
	object found = map:has(colormap, lower(hexcolor)) 
 
	if not found then 
		crash("major malfunction in get_colorname_orhex="&hexcolor) 
	else 
		hc = map:get(colormap, lower(hexcolor)) 
	end if  
 
	return hc 
end function 
 
 
-- create map of colornames using hex as index 
-- add to map the inverse map  
-- so can lookup by name to get hex or hex to get name 
-- rather than an inverse map, 
-- easier just to add the hex index while creating the names map 
-- 
-- use existing rgb routines to convert to from hex 
-- I can't imagine this would ever be a bottleneck 
-- if so could add rgb or other colorspace as indexes 
 
-- fancier: 
-- add code so can lookup nearest name from any hex 
 
function initcolors() 
	map dict = map:new() 
 
	-- x11 color names sorted by hue 
	object hexnames = split(` 
________LavenderBlush FFF0F5 
		SeaShell FFF5EE 
		Linen FAF0E6 
		MistyRose FFE4E1 
		PeachPuff FFDAB9 
		RosyBrown BC8F8F 
		Pink FFC0CB 
		LightPink FFB6C1 
		PaleVioletRed D87093 
		DarkSalmon E9967A 
		Sienna A0522D 
		HotPink FF69B4 
		LightSalmon FFA07A 
		SaddleBrown 8B4513 
		LightCoral F08080 
		Peru CD853F 
		SandyBrown F4A460 
		IndianRed CD5C5C 
		Salmon FA8072 
		Brown A52A2A 
		Maroon 800000 
		Chocolate D2691E 
		Coral FF7F50 
		FireBrick B22222 
		DarkRed 8B0000 
		Tomato FF6347 
		Crimson DC143C 
		OrangeRed FF4500 
		Red FF0000 
		MintCream F5FFFA 
		HoneyDew F0FFF0 
		DarkSeaGreen 8FBC8F 
		SeaGreen 2E8B57 
		LightGreen 90EE90 
		PaleGreen 98FB98 
		MediumSeaGreen 3CB371 
		ForestGreen 228B22 
		DarkGreen 006400 
		Green 008000 
		LimeGreen 32CD32 
		SpringGreen 00FF7F 
		LawnGreen 7CFC00 
		Chartreuse 7FFF00 
		Lime 00FF00 
		Lavender E6E6FA 
		LightSteelBlue B0C4DE 
		LightSlateGray 778899 
		SlateGray 708090 
		DarkSlateBlue 483D8B 
		MediumPurple 9370D8 
		MidnightBlue 191970 
		CornflowerBlue 6495ED 
		SlateBlue 6A5ACD 
		MediumSlateBlue 7B68EE 
		RoyalBlue 4169E1 
		Navy 000080 
		DarkBlue 00008B 
		MediumBlue 0000CD 
		Blue 0000FF 
		AliceBlue F0F8FF 
		Azure F0FFFF 
		LightCyan E0FFFF 
		LightBlue ADD8E6 
		PowderBlue B0E0E6 
		PaleTurquoise AFEEEE 
		DarkSlateGray 2F4F4F 
		CadetBlue 5F9EA0 
		SkyBlue 87CEEB 
		LightSkyBlue 87CEFA 
		MediumAquaMarine 66CDAA 
		SteelBlue 4682B4 
		Aquamarine 7FFFD4 
		MediumTurquoise 48D1CC 
		Teal 008080 
		LightSeaGreen 20B2AA 
		Turquoise 40E0D0 
		DarkCyan 008B8B 
		DodgerBlue 1E90FF 
		MediumSpringGreen 00FA9A 
		DarkTurquoise 00CED1 
		DeepSkyBlue 00BFFF 
		Aqua 00FFFF 
		Cyan 00FFFF 
		Thistle D8BFD8 
		Plum DDA0DD 
		Violet EE82EE 
		Orchid DA70D6 
		MediumOrchid BA55D3 
		Indigo 4B0082 
		DarkOrchid 9932CC 
		BlueViolet 8A2BE2 
		Purple 800080 
		DarkMagenta 8B008B 
		MediumVioletRed C71585 
		DarkViolet 9400D3 
		DeepPink FF1493 
		Fuchsia FF00FF 
		Magenta FF00FF 
		FloralWhite FFFAF0 
		Ivory FFFFF0 
		OldLace FDF5E6 
		Beige F5F5DC 
		AntiqueWhite FAEBD7 
		LightYellow FFFFE0 
		Cornsilk FFF8DC 
		PapayaWhip FFEFD5 
		LightGoldenRodYellow FAFAD2 
		BlanchedAlmond FFEBCD 
		LemonChiffon FFFACD 
		Bisque FFE4C4 
		Wheat F5DEB3 
		Moccasin FFE4B5 
		Tan D2B48C 
		PaleGoldenRod EEE8AA 
		NavajoWhite FFDEAD 
		DarkOliveGreen 556B2F 
		BurlyWood DEB887 
		DarkKhaki BDB76B 
		Khaki F0E68C 
		OliveDrab 6B8E23 
		YellowGreen 9ACD32 
		Olive 808000 
		DarkGoldenRod B8860B 
		GreenYellow ADFF2F 
		GoldenRod DAA520 
		DarkOrange FF8C00 
		Orange FFA500 
		Gold FFD700 
		Yellow FFFF00 
		White FFFFFF 
		WhiteSmoke F5F5F5 
		Snow FFFAFA 
		Gainsboro DCDCDC 
		GhostWhite F8F8FF 
		LightGray D3D3D3 
		Silver C0C0C0 
		DarkGray A9A9A9 
		Gray 808080 
		DimGray 696969 
		Black 000000 
		`, "\n", 1) 
	 
	for i=1 to length(hexnames) do 
		object name = split(hexnames[i])		 
		if 2 != length(name) then continue end if 
		 
		map:put(dict, lower(name[1]), '#'&lower(name[2]) ) 
		map:put(dict, '#'&lower(name[2]), lower(name[1]) ) 
	end for 
	 
	--?map:size(dict)  
 
	return dict 
end function 
 
map color_name_map = initcolors() 
  
 
printf(1,"black = %s from black\n" 
	, {get_colorname_orhex(color_name_map, "black")} ) 
printf(1,"%s = #000000 from #000000\n" 
	, {get_colorname_orhex(color_name_map, "#000000")} ) 
 
 
printf(1,"Turquoise = %s from Turquoise\n" 
	, {get_colorname_orhex(color_name_map, "Turquoise")} ) 
printf(1,"%s = #40E0D0 from #40E0D0\n" 
	, {get_colorname_orhex(color_name_map, "#40E0D0")} ) 
 
 
--?1/0 
 
/* 
needs some work to handle input hex as a number if that's required 
with appropriate error checking and default fallbacks 
it's indexed as a hex string 
might be nice to have uppercase hexstring 
but it just adds complication 
can use uppercase, it lowers it when getting from the map 
same with the mixed case color names.  
not sure how important that is to try and preserve. 
could store the prefered output format and return it if asked? 
lower case everything for now 
first cut proof of concept anyway 
 
 
*/