Python Forum
Converting string to hex triplet
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting string to hex triplet
#1
Been racking my brain on how to convert string color to hex triplet.
Example convert gold to ffd700.
I know that for rgb this is ff(red) d7(green) 00(blue).

I can get the 8 bit hex and 16 bit hex values but, can't for the life of me figure out how to get the hex triplet. Any direction or guidance much appreciated.


My convert func for string to hex

def colors(color):
    color = color.encode('utf-8')
    hex_color = color.hex()
    print(hex_color)

# or using 16 bit

def colors(color):
    color = color.encode('utf-16')
    hex_color = color.hex()
    print(color)

colors('golden')
Output:
676f6c64656e #output 2 b'\xff\xfeg\x00o\x00l\x00d\x00e\x00n\x00'
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
Maybe webcolors can help

Quote:What this module supports
The mappings and functions within this module support the following methods of specifying sRGB colors, and conversions between them:
  • Six-digit hexadecimal.
  • Three-digit hexadecimal.
  • Integer rgb() triplet.
  • Percentage rgb() triplet.
  • Varying selections of predefined color names (see below).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
What do you mean that "you can get the 8 bit hex"? It looks like your code is just converting each character of the word (like "golden") into a UTF-8 byte and then into a hex representation of it. So the character "g" will always be "67" in UTF-8. This has nothing to do with colors.

>>> "g".encode().hex()
'67'
Try the webcolors module.

>>> import webcolors
>>> webcolors.name_to_hex("gold")
'#ffd700'
Reply
#4
In linux, there is a command showrgb that you could call from python
>>> import csv, subprocess
>>> p = subprocess.Popen('showrgb', stdout=subprocess.PIPE)
>>> database = list(csv.reader((x.decode() for x in p.stdout), delimiter='\t'))
>>> d = {x[-1]: tuple(int(y) for y in x[0].strip().split()) for x in database}
>>> d['orchid4']
(139, 71, 137)
Reply
#5
PIL has a colormap dictionary.
from PIL import ImageColor

def print_color_info(name):
    color = ImageColor.colormap[name]
    print(f'{name} = {color}, RGB = {color[1:3]},{color[3:5]},{color[5:7]}')

print_color_info('red')
print_color_info('green')
print_color_info('blue')
print_color_info('orange')
print_color_info('yellow')
Output:
red = #ff0000, RGB = ff,00,00 green = #008000, RGB = 00,80,00 blue = #0000ff, RGB = 00,00,ff orange = #ffa500, RGB = ff,a5,00 yellow = #ffff00, RGB = ff,ff,00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting DateTime string and and converting it from AM/PM to 24 hours tester_V 2 175 Jun-08-2024, 05:16 PM
Last Post: tester_V
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 1,995 May-13-2022, 03:44 PM
Last Post: deanhystad
  Need help converting string to int dedesssse 7 2,817 Jul-07-2021, 09:32 PM
Last Post: deanhystad
  Beautify dictionary without converting to string. sharoon 6 3,534 Apr-11-2021, 08:32 AM
Last Post: buran
  how to deal with problem of converting string to int usthbstar 1 2,087 Jan-05-2021, 01:33 PM
Last Post: perfringo
  Triplet Combinations of All Values quest 2 2,084 Nov-05-2020, 09:22 AM
Last Post: quest
  converting string object inside a list into an intiger bwdu 4 2,745 Mar-31-2020, 10:36 AM
Last Post: buran
  Converting query string as a condition for filter data. shah_entrance 1 1,869 Jan-14-2020, 09:22 AM
Last Post: perfringo
  Converting a patterned string or text into excel table soup1987 1 2,190 Oct-03-2019, 01:37 AM
Last Post: Larz60+
  converting array to and from string in python 3.7.2 srm 5 6,406 Jul-03-2019, 01:11 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020