简体   繁体   English

如何更改pyglet窗口的颜色

[英]how to change the color of a pyglet window

I am creating a program which must change the color of individual pixels in a pyglet window.我正在创建一个程序,该程序必须更改 pyglet 窗口中单个像素的颜色。 I am unable to find any way to do this in the docs.我无法在文档中找到任何方法来做到这一点。 Is there a way to do this?有没有办法做到这一点?

For funsies, I'll add another answer that is more along the lines of what you might need.对于有趣的人,我将添加另一个更符合您可能需要的答案。 Because the window itself will be whatever "clear" color buffer you decide via:因为窗口本身将是您通过以下方式决定的任何“清晰”颜色缓冲区:

window = pyglet.window.Window(width=width, height=height)
pyglet.gl.glClearColor(0.5,0,0,1) # Note that these are values 0.0 - 1.0 and not (0-255).

So changing the background is virtually impossible because it's "nothing".所以改变背景几乎是不可能的,因为它“什么都没有”。
You can however draw pixels on the background via the .draw() function.但是,您可以通过.draw()函数在背景上绘制像素。

import pyglet
from random import randint

width, height = 500, 500
window = pyglet.window.Window(width=width, height=height)

@window.event
def on_draw():
    window.clear()
    for i in range(10):
        x = randint(0,width)
        y = randint(0,height)
        pyglet.graphics.draw(1, pyglet.gl.GL_POINTS,
                ('v2i', (x, y)),
                ('c3B', (255, 255, 255))
            )

pyglet.app.run()

This will create 10 randomly placed white dots on the background.这将在背景上创建 10 个随机放置的白点。
To add anything above that simply place your .blit() or .draw() features after the pyglet.graphics.draw() line.要添加上面的任何内容,只需将.blit().draw()功能放在pyglet.graphics.draw()行之后。

You could use the magic function SolidColorImagePattern and modify the data you need.您可以使用魔法函数SolidColorImagePattern并修改您需要的数据。

R,G,B,A = 255,255,255,255
pyglet.image.SolidColorImagePattern((R,G,B,A).create_image(width,height)

This is a .blit() :able image.这是一个.blit() :able 图像。 It's white, and probably not what you want.它是白色的,可能不是你想要的。
So we'll do some more wizardry and swap out all the pixels for random ones (War of the ants):所以我们会做一些更多的魔法并将所有像素换成随机像素(蚂蚁之战):

import pyglet
from random import randint

width, height = 500, 500
window = pyglet.window.Window(width=width, height=height)
image = pyglet.image.SolidColorImagePattern((255,255,255,255)).create_image(width, height)

data = image.get_image_data().get_data('RGB', width*3)
new_image = b''

for i in range(0, len(data), 3):
    pixel = bytes([randint(0,255)]) + bytes([randint(0,255)]) + bytes([randint(0,255)])
    new_image += pixel

image.set_data('RGB', width*3, new_image)

@window.event
def on_draw():
    window.clear()
    image.blit(0, 0)

pyglet.app.run()

For educational purposes, I'll break it down into easier chunks.出于教育目的,我会将其分解为更简单的部分。

image = pyglet.image.SolidColorImagePattern((255,255,255,255)).create_image(width, height)

Creates a solid white image, as mentioned.如前所述,创建纯白色图像。 It's width and height matches the window-size.它的宽度和高度与窗口大小相匹配。

We then grab the image data:然后我们抓取图像数据:

data = image.get_image_data().get_data('RGB', width*3)

This bytes string will contain width*height*<format> , meaning a 20x20 image will be 1200 bytes big because RGB takes up 3 bytes per pixel.bytes字符串将包含width*height*<format> ,这意味着20x20图像将有1200个字节大,因为 RGB 每个像素占用 3 个字节。

new_image = b''

for i in range(0, len(data), 3):
    pixel = bytes([randint(0,255)]) + bytes([randint(0,255)]) + bytes([randint(0,255)])
    new_image += pixel

This whole block loops over all the pixels (len(data) is just a convenience thing, you could do range(0, width*height*3, 3) as well, but meh.这整个块循环所有像素(len(data) 只是一个方便的事情,你也可以做range(0, width*height*3, 3) ,但是meh。
The pixel contists of 3 randint(255) bytes objects combined into one string like so:像素由 3 个 randint(255) 字节对象组合成一个字符串,如下所示:

pixel = b'xffxffxff'

That's also the reason for why we step 3 in our range(0, len(data), 3) .这也是我们在range(0, len(data), 3)3步的原因。 Because one pixel is 3 bytes "wide".因为一个像素是 3 个字节“宽”。

Once we've generated all the pixels (for some reason the bytes object image can't be modified.. I could swear I've modified bytes "strings" before.. I'm tired tho so that's probably a utopian dream or something.一旦我们生成了所有像素(由于某种原因,字节对象image无法修改..我可以发誓我以前修改过字节“字符串”..我很累,所以这可能是一个乌托邦式的梦想或其他东西.
Anyhow, once all that sweet image building is done, we give the image object it's new data by doing:无论如何,一旦完成所有甜蜜的图像构建,我们通过执行以下操作为图像对象提供新数据:

image.set_data('RGB', width*3, new_image)

And that's it.就是这样。 Easy as butter in sunshine on a -45 degree winter day.在 -45 度的冬日,阳光下的黄油很容易。

Docs:文档:

You can also opt in to get a region, and just modify a region.. But I'll leave the tinkering up to you :)你也可以选择加入一个区域,然后修改一个区域..但我会把修补工作留给你:)

You can blit pixels into background 'image'.您可以将像素点到背景“图像”中。 You can look at this Stack Overflow question.你可以看看这个堆栈溢出问题。

If you mean background color, I can help.如果您的意思是背景颜色,我可以提供帮助。 There is one option that I know of, the pyglet.gl.glClearColor function.我知道有一个选项,即 pyglet.gl.glClearColor 函数。

for example,:例如,:

import pyglet
from pyglet.gl import glClearColor

win = pyglet.window.Window(600, 600, caption = "test")

glClearColor(255, 255, 255, 1.0) # red, green, blue, and alpha(transparency)
def on_draw():
    win.clear()

That will create a window with a white background(as opposed to the default, black)这将创建一个白色背景的窗口(而不是默认的黑色)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM