Pygtk 图形上下文和分配颜色
我对此进行了搜索,但没有任何内容符合我的要求。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Drawable 和 GC 过去。 这个答案让我开始寻找解决方案。 这是一个使用自定义颜色 gc 绘制一些正方形的简单示例:
希望它有帮助。
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:
Hope it helps.
问题在于
NewColor()
函数返回未分配的颜色c
。colormap.alloc_color()
返回一个gtk.gdk.Color
,它是分配的颜色。 要解决这个问题,NewColor()
中的最后一行应该是:The problem is that the
NewColor()
function is returning an unallocated colorc
.colormap.alloc_color()
returns agtk.gdk.Color
which is the allocated color. To fix things the last line inNewColor()
should be: