Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pygame.Color()返回的神秘值_Python_Colors_Pygame - Fatal编程技术网

Python pygame.Color()返回的神秘值

Python pygame.Color()返回的神秘值,python,colors,pygame,Python,Colors,Pygame,我需要识别从像素数组中的元素中采样的像素颜色,然后输入pygame.color()。pygame.Color()返回的一个值让我感到困惑 我研究了像素阵列中选定元素中的整数值,将该值转换为二进制并识别RGB组件。这三个部分都可以解释。调用pygame.Color(pixel_integer)返回所有三个值以及(0,R,G,B)中的一个额外值(零)。那个零让我困惑。有人能确定这个值代表什么吗。颜色饱和二者都也不完全是别的吗?为什么是零 from os import system import py

我需要识别从像素数组中的元素中采样的像素颜色,然后输入pygame.color()。pygame.Color()返回的一个值让我感到困惑

我研究了像素阵列中选定元素中的整数值,将该值转换为二进制并识别RGB组件。这三个部分都可以解释。调用pygame.Color(pixel_integer)返回所有三个值以及(0,R,G,B)中的一个额外值(零)。那个零让我困惑。有人能确定这个值代表什么吗。颜色饱和二者都也不完全是别的吗?为什么是零

from os import system
import pygame


system('cls') # Clear greeting from CC screen.

usecolor = (46,12,187,255)       # Declare our chosen color.
sprite = pygame.Surface((10,10)) # Greate a surface. Let us call it a 'sprite'.
sprite.fill(usecolor)            # Fill the 'sprite' with our chosen color.

array = pygame.PixelArray(sprite)   # Create a pixel array of the sprite, locking the sprite.
sample = array[5,5]                 # Sample the integer holding the color values of pixel [5,5]
del array                           # Delete the pixel array, unlocking the sprite.

print("\n Challenge: Use a pixel-array of a sprite to sample the color of the pixel at location [5,5]")
print("            Determine the RGB-values held by the integer found there.") 

print("\n We have created a 100x100 'sprite' and filled it with the color "+str(usecolor))
print(" Then we cerated a pixel array of the sprite and sampled the color of the element at location [5,5],")
print(" corrosponding to pixel [5,5] on the sprite.")

print("\n The color we used to fill the sprite was",usecolor)
print(" The value sampled from pixel [5,5] is",sample)
print(" pygame.Color(sample) =",pygame.Color(sample))

print("\n Notice that there is a mystery zero at the front of the values returned by pygame.Color().")
print(" Notice too that our Alpha value is missing from the back.")
print(" (Let us ignore the missing alpha value. It doesn't matter.)")

print("\n m,r,g,b = pygame.Color(sample) where 'm' stands for 'mystery'")
m,r,g,b = pygame.Color(sample)

print("\n m >>",m)
print(" r >>",r)
print(" g >>",g)
print(" b >>",b)
color = (r,g,b)

print("\n From these values we find that color =",color)

print("\n Question: What is 'm'?")
print("\n 'm' can't be alpha because that value would be 255 (opaque); an alpha value of 0 is transparent.")
print(" Besides, the alpha value should be at the end of the color series, not at the beginning. Any ideas?")
print("\n\n\tThanks!\n\n       -Science_1")

print('',end="\n ")
exit()

没有错误,但是铅的零值让我感到困惑。是什么原因造成的?它意味着什么?

根据提供的信息,所讨论的像素很可能是黑色或空的。

在文档中,您可以看到它是alpha通道

但是如果屏幕不能在32位模式下工作-
RGBA
,则
Surface
不必使用
alpha

您可以设置32位模式

pygame.display.set_mode( ..., depth=32)
您还可以使用
将曲面转换为
RGBA
。convert_alpha()

但只有使用
set\u mode()
时,它才起作用

此代码显示
alpha=255
。但如果删除
.convert_alpha()
,则它会再次显示
alpha=0

import pygame

pygame.init()
pygame.display.set_mode((800, 600), depth=32)

usecolor = (46, 12, 187, 255)
sprite = pygame.Surface((10,10)).convert_alpha()
sprite.fill(usecolor)

array = pygame.PixelArray(sprite)
sample = array[5,5]

a,r,g,b = pygame.Color(sample)

print(" pygame.Color(sample) =", pygame.Color(sample))
print(" r >>", r)
print(" g >>", g)
print(" b >>", b)
print(" a >>", a)

pygame.quit()

谢谢你,福拉斯,这很有趣。我将尝试将深度设置为32,然后用它进行实验,看看神秘的零是否确实是alpha值。这就是alhpa失踪的原因。(奇怪的是,它会出现在RGB值的前面而不是末尾。)PS:我的程序中有很多你的代码,但是为了问我的问题,我把它去掉了(还有更多的代码);但我从未设置过深度,也没有尝试转换,这可能是关键。.Color()的参考资料在价值/订购问题上含糊其辞,让我摸不着头脑。再次感谢。convert_alpha()可以让alpha值显示出来。像素数组返回一个负值(设置的高位),pygame.Color()会阻塞负值并消失,但是命令sprite.get_at((5,5))返回的(R,G,B,a)值很好。在我使用的像素阵列方法(alpha值为零)和您提供的答案之间,我相信我已经掌握了所有我需要的方法。解码负值以便将其输入pygame.Color()只是另一天的繁忙工作。我认为我目前的问题得到了回答。谢谢
import pygame

pygame.init()
pygame.display.set_mode((800, 600), depth=32)

usecolor = (46, 12, 187, 255)
sprite = pygame.Surface((10,10)).convert_alpha()
sprite.fill(usecolor)

array = pygame.PixelArray(sprite)
sample = array[5,5]

a,r,g,b = pygame.Color(sample)

print(" pygame.Color(sample) =", pygame.Color(sample))
print(" r >>", r)
print(" g >>", g)
print(" b >>", b)
print(" a >>", a)

pygame.quit()