Python Forum
RGB() <> colorchooser.askcolor
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RGB() <> colorchooser.askcolor
#1
I pick blue color but the value returned is red, not blue as you can see in the code comments. Should I parse out the RGB values from the picked color and use RBG() to get the right value for use outside of tkinter?

The value from colorchooser.askcolor is fine for setting colors for a label in tkinter.

# -*- coding: utf-8 -*-
from ctypes.wintypes import RGB
from tkinter import *
from tkinter import colorchooser

color = RGB(0, 0, 255)  # Blue
print(color)            # 16711680 = #FF000 = Blue
print(RGB(255,0,0))     # 255 = Red

def choose_color():
 
    # variable to store hexadecimal code of color
    color_code = colorchooser.askcolor(title ="Choose color")
    #rgbTuplet, colourString= colorchooser.askcolor(title ="Choose color")
    print(color_code[0])    #   (0, 0, 255)
    print(color_code[1])    #   #0000ff
    print(int(color_code[1].replace('#',''), 16))  # 255, red by RGB()
 
root = Tk()
button = Button(root, text = "Select color",
                   command = choose_color)
button.pack()
root.geometry("50x50")
root.mainloop()
Reply
#2
On windows 10 I choose Blue and get back
Output:
(0, 0, 255)
Reply
#3
Yes, I showed that in the comments. But, look at the numerical value #0000ff. RGB() shows it as: 16711680 which is #FF000. The value shown by the askcolor is #0000ff which is 255.

Do you see my issue now?
Reply
#4
It is plausible that Windows' RGB() function stores the red at the least significant byte. You could define
def RGB(r, g, b):
    return (((b << 8) | g) << 8) | r

...
print(RGB(*color_code[0]))
Reply
#5
Windows uses a different order than Python for colors. In Python red is 16711680 and in windows red is 255. Why do you think this matters at all? Are you trying to pass integer color values to a windows DLL?
Reply
#6
(Aug-24-2022, 05:17 PM)deanhystad Wrote: Windows uses a different order than Python for colors. In Python red is 16711680 and in windows red is 255. Why do you think this matters at all? Are you trying to pass integer color values to a windows DLL?
Reply
#7
Yes, I am passing the integer to Windows using SetWinColors.
Reply
#8
Then you will want to get the individual red, green, blue components of the color and use them with the windows RGB color macro. This is the recommendation when using SetSysColors (winuser.h).
You do not want to send the Python color directly.

32 bit color values are meaningless without context. Just like 32bit integer values, or floats, or any multi-byte data.
Reply
#9
Thanks, I just found it odd. This closes my question.

I can achieve my goal by using RBG() with:
r, g, b = color_code[0]
Reply
#10
FYI, see any differences here
color = RGB(0, 0, 255)  # Blue
print(RGB(255,0,0))     # 255 = Red
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 2,176 Jul-06-2022, 01:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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