Quote:
I did expect the output of ColourNameToRGB() to return the same name when passed to RGBColourToName().
But it does do that! Try running this script in the immediate window:
for k, v in pairs (colour_names) do
local value = ColourNameToRGB(k) -- turn into a number
if v ~= value then
print ("Incorrect conversion for colour", k)
end
local name = RGBColourToName (value) -- turn back into a name
if name ~= k then
print (string.format ("Colour value 0x%06x converted to %s rather than %s", value, name, k))
end -- if
end -- for
Output:
Colour value 0xd3d3d3 converted to lightgrey rather than lightgray
Colour value 0xffff00 converted to cyan rather than aqua
Colour value 0xff00ff converted to magenta rather than fuchsia
There are two tests there: First is that every colour name, after converting to a number, is converted to the same value as is stored internally in the colour_names table.
Every single one passes!
The second is that every colour number, after converting to a name, converts back to the same name as originally. There are three failures there, because those three colours listed have the same codes (ie. lightgrey is the same as lightgray). Thus it has to choose one or the other. All the other pass.
Your confusion is that you think that #123456 is the same as 0x123456 when it just isn't. One is an HTML code for a colour, the other is the way numbers are stored in C. Because of endianness, they are not equivalent.
Here's a
post on Stack Overflow by someone who is getting the "wrong" colours because he tries to use actual numbers rather than the HTML colours.
Here's
another post about colours and endianness.
MUSHclient isn't wrong, it is just not storing colours the way
you are expecting it to. Hence when you use the 0xRRGGBB notation you are getting the wrong results.
Its conversion to and from numbers to names is consistent, as the example above shows.
Here is another example showing converting to and from HTML codes:
for i = 1, 5000 do
local HTMLname = string.format ("#%06X", i)
local value = ColourNameToRGB(HTMLname) -- convert to number
local name = RGBColourToName (value) -- convert back to name
if name ~= HTMLname then
print (string.format ("Colour value 0x%06x converted to %s rather than %s", value, name, HTMLname))
end -- if
end -- for
print "done"
Output:
Colour value 0x800000 converted to navy rather than #000080
Colour value 0x8b0000 converted to darkblue rather than #00008B
Colour value 0xcd0000 converted to mediumblue rather than #0000CD
Colour value 0xff0000 converted to blue rather than #0000FF
done
Of the 5000 colours tested (ie. #000001, #000002 and so on) all but four convert exactly back to what they started. Those four (listed above) "fail" because an equivalent name was found so that was used instead.
You will notice above that the actual RGB codes are reversed, as expected. One is done by doing a string.format in hex of the underlying code. The other is the HTML code, so the byte order is different.