Pygtk 图形上下文和分配颜色

发布于 2024-07-23 03:21:08 字数 2544 浏览 6 评论 0原文

我对此进行了搜索,但没有任何内容符合我的要求。

http://www.mail-archive.com/[电子邮件受保护]/msg10529.html -- 没有人回复他。 这正是我所经历的。 当我在图形上下文上设置前景时,它似乎并没有真正改变。

我已经阅读了教程和常见问题解答,但都没有说太多。 他们要么只使用黑白上下文,要么给你损坏的链接。 我想也许这是一个错误。 但我内心的孩子说我只是错过了一些东西,而我一直忽视我有一个可行的替代方案这一事实。 不过这样会更好。 我越深入地了解这一点,我就越需要这些背景和颜色。

这是我的代码片段。

def CreatePixmapFromLCDdata(lcdP, ch, widget):
    width = lcdP.get_char_width()
    height = lcdP.get_char_height()

    # Create pixmap
    pixmap = gtk.gdk.Pixmap(widget.window, width, height)

    # Working graphics contexts, wrong color
    black_gc = widget.get_style().black_gc
    white_gc = widget.get_style().white_gc

    char_gc = widget.window.new_gc()
    colormap = char_gc.get_colormap()

    bg_color = NewColor(text="#78a878", colormap=colormap)

    print "Before", char_gc.foreground.red, char_gc.foreground.green, char_gc.foreground.blue
    char_gc.set_foreground(bg_color)
    print "AFter", char_gc.foreground.red, char_gc.foreground.green, char_gc.foreground.blue

    fg_color = NewColor(text="#113311", colormap=colormap)

    pixmap.draw_rectangle(char_gc, True, 0, 0, width, height)
    char_gc.foreground = fg_color
    for j in range(lcdP.dots['y']):
        k = lcdP.pixels['y']*j
        for i in range(lcdP.dots['x']):
            if 1<<(lcdP.dots['x']-1-i) & ch[j] == 0: continue

            m = i*lcdP.pixels['y']

            for jj in range(k, k+lcdP.pixels['y']-1):
                for ii in range(m+1, m+lcdP.pixels['x']):
                    pixmap.draw_point(char_gc, ii, jj)
    return pixmap

我想这可能是我分配颜色的方式。 正如您在代码片段中看到的,我使用了图形上下文自己的颜色图。 我尝试过不同的颜色图,这是最新的。 我什至尝试过未分配的颜色。 注意white_gc 和black_gc 图形上下文。 当我使用它们时,我可以在白色背景上很好地绘制黑色。 否则(在创建的上下文中)一切都是黑色的,fg和bg。 当我改变白色的前景色时,它总是显示为黑色。

这是输出。 请注意,颜色变化不大。 我想说它没有改变,或者至少不足以在视觉上产生影响。

Before 6 174 60340
After 5 174 60340

这是我分配颜色的方法。

def NewColor(red=0, green=0, blue=0, text=None, colormap=None):
    if text == None:
        c = gtk.gdk.Color(red, green, blue)
    else:
        c = gtk.gdk.color_parse(text)
    if colormap == None:
        colormap = gtk.gdk.colormap_get_system()
    colormap.alloc_color(c)
    return c

I've searched on this, but nothing has what I'm looking for.

http://www.mail-archive.com/[email protected]/msg10529.html -- Nobody answered him. This is exactly what I'm experiencing. When I set the foreground on a graphics context, it doesn't seem to actually change.

I've been through the tutorial and FAQ, but neither say much. They either just use the black and white contexts or they give you broken links. I'm left thinking maybe it's a bug. But the kid in me says I'm just missing something and I keep ignoring the fact I have a working alternative. This would be better though. And the further I get into this, the more I'm going to need these contexts and colors.

Here's my code snippet.

def CreatePixmapFromLCDdata(lcdP, ch, widget):
    width = lcdP.get_char_width()
    height = lcdP.get_char_height()

    # Create pixmap
    pixmap = gtk.gdk.Pixmap(widget.window, width, height)

    # Working graphics contexts, wrong color
    black_gc = widget.get_style().black_gc
    white_gc = widget.get_style().white_gc

    char_gc = widget.window.new_gc()
    colormap = char_gc.get_colormap()

    bg_color = NewColor(text="#78a878", colormap=colormap)

    print "Before", char_gc.foreground.red, char_gc.foreground.green, char_gc.foreground.blue
    char_gc.set_foreground(bg_color)
    print "AFter", char_gc.foreground.red, char_gc.foreground.green, char_gc.foreground.blue

    fg_color = NewColor(text="#113311", colormap=colormap)

    pixmap.draw_rectangle(char_gc, True, 0, 0, width, height)
    char_gc.foreground = fg_color
    for j in range(lcdP.dots['y']):
        k = lcdP.pixels['y']*j
        for i in range(lcdP.dots['x']):
            if 1<<(lcdP.dots['x']-1-i) & ch[j] == 0: continue

            m = i*lcdP.pixels['y']

            for jj in range(k, k+lcdP.pixels['y']-1):
                for ii in range(m+1, m+lcdP.pixels['x']):
                    pixmap.draw_point(char_gc, ii, jj)
    return pixmap

I thought maybe it was the way I was allocating the colors. As you see in the snippet, I've used the graphic context's own colormap. I've tried different colormaps, this being the latest. I've even tried an unallocated color. Notice the white_gc and black_gc graphics contexts. When I use those I'm able to draw black on a white background fine. Otherwise (with a created context) everything's black, fg and bg. When I change white's foreground color, it always comes out black.

Here's the output. Notice the color doesn't change very much. I'd say it didn't change, or at least not enough to matter visually.

Before 6 174 60340
After 5 174 60340

Here's how I allocate the colors.

def NewColor(red=0, green=0, blue=0, text=None, colormap=None):
    if text == None:
        c = gtk.gdk.Color(red, green, blue)
    else:
        c = gtk.gdk.color_parse(text)
    if colormap == None:
        colormap = gtk.gdk.colormap_get_system()
    colormap.alloc_color(c)
    return c

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-07-30 03:21:08

我在 DrawableGC 过去。 这个答案让我开始寻找解决方案。 这是一个使用自定义颜色 gc 绘制一些正方形的简单示例:

import gtk

square_sz = 20
pixmap = None
colour = "#FF0000"
gc = None

def configure_event( widget, event):
    global pixmap
    x, y, width, height = widget.get_allocation()
    pixmap = gtk.gdk.Pixmap(widget.window, width, height)
    white_gc = widget.get_style().white_gc
    pixmap.draw_rectangle(white_gc, True, 0, 0, width, height)
    return True

def expose_event(widget, event):
    global pixmap
    if pixmap:
        x , y, w, h = event.area
        drawable_gc = widget.get_style().fg_gc[gtk.STATE_NORMAL]
        widget.window.draw_drawable(drawable_gc, pixmap, x, y, x, y, w, h)
    return False

def button_press_event(widget, event):
    global pixmap, square_sz, gc, colour
    if event.button == 1 and pixmap:
        x = int(event.x / square_sz) * square_sz
        y = int(event.y / square_sz) * square_sz
        if not gc:
            gc = widget.window.new_gc()
            gc.set_rgb_fg_color(gtk.gdk.color_parse(colour))
        pixmap.draw_rectangle(gc, True, x, y, square_sz, square_sz)
        widget.queue_draw_area(x, y, square_sz, square_sz)

    return True

if __name__ == "__main__":
    da = gtk.DrawingArea()
    da.set_size_request(square_sz*20, square_sz*20)

    da.connect("expose_event", expose_event)
    da.connect("configure_event", configure_event)
    da.connect("button_press_event", button_press_event)

    da.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK)

    w = gtk.Window()
    w.add(da)
    w.show_all()
    w.connect("destroy", lambda w: gtk.main_quit())

    gtk.main()

希望它有帮助。

I've had some trouble with Drawable and GC in the past. This answer got me started on the way to a solution. Here's a quick example that uses a custom colour gc to draw some squares:

import gtk

square_sz = 20
pixmap = None
colour = "#FF0000"
gc = None

def configure_event( widget, event):
    global pixmap
    x, y, width, height = widget.get_allocation()
    pixmap = gtk.gdk.Pixmap(widget.window, width, height)
    white_gc = widget.get_style().white_gc
    pixmap.draw_rectangle(white_gc, True, 0, 0, width, height)
    return True

def expose_event(widget, event):
    global pixmap
    if pixmap:
        x , y, w, h = event.area
        drawable_gc = widget.get_style().fg_gc[gtk.STATE_NORMAL]
        widget.window.draw_drawable(drawable_gc, pixmap, x, y, x, y, w, h)
    return False

def button_press_event(widget, event):
    global pixmap, square_sz, gc, colour
    if event.button == 1 and pixmap:
        x = int(event.x / square_sz) * square_sz
        y = int(event.y / square_sz) * square_sz
        if not gc:
            gc = widget.window.new_gc()
            gc.set_rgb_fg_color(gtk.gdk.color_parse(colour))
        pixmap.draw_rectangle(gc, True, x, y, square_sz, square_sz)
        widget.queue_draw_area(x, y, square_sz, square_sz)

    return True

if __name__ == "__main__":
    da = gtk.DrawingArea()
    da.set_size_request(square_sz*20, square_sz*20)

    da.connect("expose_event", expose_event)
    da.connect("configure_event", configure_event)
    da.connect("button_press_event", button_press_event)

    da.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK)

    w = gtk.Window()
    w.add(da)
    w.show_all()
    w.connect("destroy", lambda w: gtk.main_quit())

    gtk.main()

Hope it helps.

┊风居住的梦幻卍 2024-07-30 03:21:08

问题在于 NewColor() 函数返回未分配的颜色 ccolormap.alloc_color() 返回一个 gtk.gdk.Color,它是分配的颜色。 要解决这个问题,NewColor() 中的最后一行应该是:

return colormap.alloc_color(c)

The problem is that the NewColor() function is returning an unallocated color c. colormap.alloc_color() returns a gtk.gdk.Color which is the allocated color. To fix things the last line in NewColor() should be:

return colormap.alloc_color(c)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文