I am looking for a good way to convert a colour from it's name to the hex value, i.e. convert "red" to "#FF0000", etc.
Thanks!
Thanks!
This forum is a read-only archive of the Gammon Software forum (2000–2026). No new posts can be made. Search the archive.
Posted by Rene on Mon 01 Oct 2018 10:50 PM — 5 posts, 22,898 views.
function ColourNameToRGBHex(color_name)
color = ColourNameToRGB(color_name)
low = math.floor(color/65536)
color = color - low * 65536
mid = math.floor(color/256) * 256
color = color - mid
high = color * 65536
return string.format("#%06x", high + mid + low)
end
Yes, but you asked for one number.
See https://en.wikipedia.org/wiki/Web_colors#HTML_color_names
The standard HTML colour codes are like this:
#rrggbb
Where “rr” is the red component (0 to 0xFF, that is, 0 to 255) and so on.
Full green is actually “lime” and not “green”. Lime would give you #FF00