1

I've inserted a gtk.ColorButton() but when I retrieve the value (get_color) I obtain a 12 digits hex string (for certain colors). The problem is that I need it to give me only 6 digits.

Here's a snippet :

import pygtk
pygtk.require("2.0")

import gtk

class TestColorButton(object):

    def __init__(self):
        self.win = gtk.Window()
        self.colorbutton = gtk.ColorButton()
        container = gtk.VBox()
        Button = gtk.Button("Get color")
        Button.connect("released", self.get_selected_color)
        container.pack_start(self.colorbutton)
        container.pack_start(Button)
        self.win.add(container)
        self.win.show_all()

    def get_selected_color(self, widget):
        print self.colorbutton.get_color()

if __name__ == "__main__":
    TestColorButton()
    gtk.main()

I don't know if the only solution would be to try to convert the 12 digits hex string to a 6 digits one (even though I would be losing information). Since nothing in python can use a 12 digits color I'm seriously wondering what was the use of this...

The funny part is that in the window that pops up I can see a 6 digits hex string. Maybe if I can get_childs that window until I find it... but it seems complicated for a simple matter...

4

3 回答 3

1

这是一个小函数,它使用 gdk.color 的 to_floats 函数来获取 [0..1] 范围内的 rgb 三元组,然后将其转换为适合 2 位十六进制的 [0..255]。

def gdk_to_hex(gdk_color):
    colors = gdk_color.to_floats()
    return "#" + "".join(["%02x" % (color * 255) for color in colors])
于 2014-02-09T00:50:11.713 回答
0

目标是为我选择的颜色获取 6 位十六进制字符串。一个临时解决方案是取每个四位颜色的前两位(#rrrrggggbbbb 给出#rrggbb)。我选择的是将每个四位颜色转换为 16 位整数(0000-FFFF 到 0-65535)将其重新缩放为 8 位(0-65535 到 0-255)将其转换回十六进制字符串(现在给出两个字符四)

我不认为它真的很聪明,但至少它给了我大致上我想要的颜色......

片段更新:

import pygtk
pygtk.require("2.0")

import gtk

class TestColorButton(object):

    def __init__(self):
        self.win = gtk.Window()
        self.colorbutton = gtk.ColorButton()
        container = gtk.VBox()
        Button = gtk.Button("Get color")
        Button.connect("released", self.get_selected_color)
        container.pack_start(self.colorbutton)
        container.pack_start(Button)
        self.win.add(container)
        self.win.show_all()

    def get_selected_color(self, widget):
        a = self.colorbutton.get_color()
        print a
        self.gdk_to_hex(a)

    def gdk_to_hex(self, gdk_color):
        colors = gdk_color.to_string()
        a =  "#"+"".join([self.fourtotwohexdigit(i) for i in (colors[1:5], colors[5:9], colors[9:13])])
        print a

    def fourtotwohexdigit(self, hexa):
        return hex(int(int(hexa,16)*255/65535))[2:]     

if __name__ == "__main__":
    TestColorButton()
    gtk.main()
于 2014-03-11T11:25:52.223 回答
-1

当您从中获取对象 gtk.gdk.Color

color = self.colorbutton.get_color()

你可以得到字符串表示

color.to_string()

12 位#fcfce9e94f4f 字符串包含每种颜色的十六进制形式的 4 位,在这种情况下红色=64764,绿色=59881,蓝色=20303

red=64764 -> fcfc
green=59881 -> e9e9
blue=20303 -> 4f4f

您还可以从 gtk.gdk.Color 打印单个颜色,例如

color.red
color.green
color.blue

或者如果您更喜欢 0.0 - 1.0 范围内的十进制值

color.red_float
color.green_float
color.blue_float
于 2013-10-11T21:26:28.420 回答