【问题标题】:Mystery value returned by pygame.Color()pygame.Color() 返回的神秘值
【发布时间】:2019-12-01 17:56:02
【问题描述】:

我需要识别从 PixelArray 中的元素采样的像素颜色,然后输入 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()

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

【问题讨论】:

    标签: python colors pygame


    【解决方案1】:

    Color 的文档中,您可以读到它是 Alpha 通道。

    但如果屏幕在 32 位模式下无法工作,Surface 不必使用 alpha 值 - RGBA

    可以设置32位模式

    pygame.display.set_mode( ..., depth=32)
    

    您还可以使用.convert_alpha() 将表面转换为RGBA

    sprite = pygame.Surface((10,10)).convert_alpha()
    

    但它仅在您使用set_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)很好。在我拥有的 PixelArray 方法(alpha 值是否领先并且读数为零)和您提供的答案之间,我相信我拥有我需要的一切。解码负值以便可以将其提供给 pygame.Color() 只是忙碌的一天。我认为我当前的问题已得到解答。谢谢!
    【解决方案2】:

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

    【讨论】: