1

Good morning/afternoon/night! I'm making a program right now and I ran into one problem. This program should create QR code from given text and should change its color to given color in RGB or HEX. But when I've made HEX color code to RGB "interpreter"... Well , you can see this problem: input - 61c3ff and output should show me something like R = 61 TO decimal (97), G = c3 TO decimal (195) and B = ff TO decimal (255). But in output I can only see that R = 6, G = 12, B = 15. What the hell? Here is a part from my code, which must recognize which one color code is given: RGB or HEX and must "translate" HEX to RGB (For example, {#}9effec in RGB will be 158 255 236).

for i in color:
    if i == " ":
        color = color.split()
        x = color[0]
        y = color[1]
        z = color[2]
    else: # If HEX color
        if color[0] == "#": # if it starts with "#"
            color = color[1:]
            decX = color[0:1]
            decY = color[2:3]
            decZ = color[4:5]
            x = int(decX, 16)
            y = int(decY, 16)
            z = int(decZ, 16)
            print(color, x, y, z)
        else: # if it's without "#"
            decX = color[0:1]
            decY = color[2:3]
            decZ = color[4:5]
            x = int(decX, 16)
            y = int(decY, 16)
            z = int(decZ, 16)
            print(color, x, y, z)

If you need full code - https://pastebin.com/yCgK2KwU

Nullman
  • 4,179
  • 2
  • 14
  • 30
Me.
  • 23
  • 5
  • See [converting-hex-to-rgb-value-in-python](https://stackoverflow.com/questions/29643352/converting-hex-to-rgb-value-in-python) – Patrick Artner Feb 21 '19 at 14:51

1 Answers1

6

Your problem is caused by wrong slicing, consider following example:

color = "61c3ff"
print(color[0:1]) #6
print(color[2:3]) #c
print(color[4:5]) #f

as you can see slicing of str in form of [t:t+1] gives str of length 1, simply increase second value in every slicing and it should work properly:

color = "61c3ff"
print(color[0:2]) #61
print(color[2:4]) #c3
print(color[4:6]) #ff
Daweo
  • 31,313
  • 3
  • 12
  • 25