[Python] Python: How to convert a rgb or hex value to single color values

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am making a Python script with a GUI using Tkinter to control LED strips with a Raspberry Pi. The strips are connected to mosfets and they connect to the GPIO ports.

    So far I have: a GUI with on/off buttons for each channel, this starts or stops the pwm output. I have a slider (scale) coupled to a entry box in which the pwm value is changed from 0 to 100, you can move the slider or type in a value in the entry box and they both change when changing one. this also automatically updates the outputs DutyCycle from 0 to 100 so you can completely dim a channel's color in 100 steps and see it dim as you move the slider.

    Now i have used the Tkinter.colorchooser module to add a button to let me pick a color with the build in colorpicker. When i pick a color, the module returns a tuple consisting of the rgb values and a hex value, what looks like this:

    {214.8359375 215.83984375 214.8359375} #d6d7d6


    I am able to take only the rgb triple or only the hex value and put it in a variable but here's where I am stuck.

    My pwm circuit uses a 0-100 value without decimals. so I started with getting just the rgb triple and divided its value by 255 and multiply by 100 to give me a 0-255 to 0-100 conversion. but it still has a lot of decimals which I don't need/want. and the 3 color value's are just separated by spaces.

    Now how can I take that triple and convert it into 3 separate variables without decimals so I can put each variable in its desired entry box and make the slider move and change the leds dutycycle.

    I've tried splitting the tuple but it gives me something like it expected less values, but I think that is because the tuple doesn't have commas

    Would it be easier to take the hex value and convert it to separate values? I hope that anyone can help me with this, I'm a little stuck right now.

    Thank you.

    Update:

    def pickcolor1():
    (triple1,hexstr1) = askcolor(initialcolor='#000000')
    if triple1:
    triple1pwm = [x / 255.0 * 100 for x in triple1]
    colorlabel1 = Label(mGui,text=triple1pwm).grid(row=14,column=0,columnspan=34)
    pickcolorbutton1 = Button(mGui,text='Pick color',width=1,command=pickcolor1).grid(row=13,column=1,columnspan=4,sticky=W+E)


    With all sliders of the colorpicker to 0 it gives this: [0.0, 0.0, 0.0]

    So I also need to remove the brackets? [ ]

    with all sliders to 255 it gives: [100.390625, 100.390625, 100.390625]

    Update2:

    I have the values separated and put into different labels, later that will be entryboxes

    def pickcolor1():
    (triple1,hexstr1) = askcolor(initialcolor='#000000')
    if triple1:
    triple1pwm = [x / 255.0 * 100 for x in triple1]
    colorlabelr1 = Label(mGui,text=triple1pwm[0]).grid(row=14,column=0,columnspan=34)
    colorlabelg1 = Label(mGui,text=triple1pwm[1]).grid(row=15,column=0,columnspan=34)
    colorlabelb1 = Label(mGui,text=triple1pwm[2]).grid(row=16,column=0,columnspan=34)
    print(triple1pwm[0])
    print(triple1pwm[1])
    print(triple1pwm[2])
    pickcolorbutton1 = Button(mGui,text='Pick color',width=1,command=pickcolor1).grid(row=13,column=1,columnspan=4,sticky=W+E)


    This works now only need to round it down

    Update3:

    Now rounded each value down and put it into a entry box, still has the .0 after the value.

    def pickcolor1():
    (triple1,hexstr1) = askcolor(initialcolor='#000000')
    if triple1:
    triplepwm1 = [x // 2.55 for x in triple1]
    pwmr1 = StringVar()
    pwmg1 = StringVar()
    pwmb1 = StringVar()
    pwmr1.set(triplepwm1[0])
    pwmg1.set(triplepwm1[1])
    pwmb1.set(triplepwm1[2])
    pickentryr1 = Entry(mGui,textvariable=pwmr1).grid(row=14,column=0,columnspan=34)
    pickentryg1 = Entry(mGui,textvariable=pwmg1).grid(row=15,column=0,columnspan=34)
    pickentryb1 = Entry(mGui,textvariable=pwmb1).grid(row=16,column=0,columnspan=34)
    print(triplepwm1[0])
    print(triplepwm1[1])
    print(triplepwm1[2])
    pickbutton1 = Button(mGui,text='Pick color',width=1,command=pickcolor1).grid(row=13,column=1,columnspan=4,sticky=W+E)

    Continue reading...

Compartilhe esta Página