-2

My question is if there is any way from Python to convert an RGB or HEX color value to the corresponding text of the color.

Example: * # FFFFFF -> WHITE * or * RGB (255,255,255) -> WHITE *

devsant13
  • 11
  • 2
  • 1
    What would you expect to get from e.g. `#ABCDEF` ? – Pranav Hosangadi Jul 13 '21 at 15:29
  • Have you got a full list of the text representations? There's a defined list of "web colours" but that's not a standard thing, just a way of assigning an arbitrary word with a hex code – Martin Jul 13 '21 at 15:29
  • You can refer from here [hex-to-rgb](https://stackoverflow.com/questions/29643352/converting-hex-to-rgb-value-in-python) – TeraCotta Jul 13 '21 at 15:30

1 Answers1

0

You would need to create a lookup table for all the colors you need:

hexToText = {'#FFFFFF':'WHITE', '#FF0000':'RED', '#00FF00':'GREEN', '#0000FF':'BLUE'}

testHex = '#FF0000'
testHexColor = hexToText[testHex] # now has 'RED'

rgbToText = {(255,255,255):'WHITE', (255,0,0):'RED', (0,255,0):'GREEN', (0,0,255):'BLUE'}

testRgb = (0,255,0)
testRgbColor = rgbToText[testRgb] # now has 'GREEN'
hypercubed
  • 13
  • 6