Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Converting Colours to HEX

Converting Colours to HEX

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Rene   (46 posts)  Bio
Date Mon 01 Oct 2018 10:50 PM (UTC)
Message
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!
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Tue 02 Oct 2018 03:30 AM (UTC)

Amended on Tue 02 Oct 2018 11:16 PM (UTC) by Fiendish

Message
modified from http://www.gammon.com.au/forum/?id=1883


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

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Rene   (46 posts)  Bio
Date Reply #2 on Tue 02 Oct 2018 06:39 PM (UTC)
Message
Thanks, but it does not seem to be working fully.
For blue it returns #ff, for green #8000

I am looking at it to try understanding it and see if I can figure out the issue. I am unsure what the general number from ColourNameToRGB means, as standard RGB give 3 numbers for the red, green and blue value, yet this returns a single number, so I assume if I understood that it would make things simpler.

Thanks so far.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 02 Oct 2018 08:50 PM (UTC)
Message

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


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #4 on Tue 02 Oct 2018 11:17 PM (UTC)
Message
I've updated it to add zero-padding.

https://github.com/fiendish/aardwolfclientpackage
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


15,744 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.