gpt4 book ai didi

graphics - 将颜色分解为其 rgb 值

转载 作者:行者123 更新时间:2023-12-04 18:13:20 26 4
gpt4 key购买 nike

我正在 xlib 中编写一个项目并且遇到了关于颜色的问题。我使用无符号长类型变量来存储颜色值。有人知道我如何获取每种颜色的红色绿色和蓝色值吗?

最佳答案

您是说 24 位颜色(每个颜色分量 8 位)一起存储在一个 32 位整数中吗?如果是这种情况,您可以使用逻辑 AND 操作将其他位归零来获取值。

假设你从

/*
alpha? r g b
00000000 10101010 10101010 10101010 your 32 bit integer might look like this
& logical AND operator
00000000 00000000 00000000 11111111 a bit mask
=
00000000 00000000 00000000 10101010 the result
so now your 32 bit integer only has the blue values.
To do this in code...
*/
unsigned char B = (unsigned char) (your_integer & 0x000000ff) //000000ff is hex version of the bit mask
//but now what about the other two colors? you can't just apply a bit mask like 0000ff00 because 00000000000000001010101000000000 is much larger than 255.

//So you have to either divide the result by 256 to shift the bits right, or use >>8 to shift them to the right.

unsigned char G = (unsigned char) ((your_integer & 0x0000ff00) / 256)
unsigned char R = (unsigned char) ((your_integer & 0x00ff0000) / 256^2)
//or using the way I've used in the past... shifting before the mask.

unsigned char G = (unsigned char) ((your_integer >> 8) & 0x000000ff)
unsigned char R = (unsigned char) ((your_integer >> 16) & 0x000000ff)

关于graphics - 将颜色分解为其 rgb 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12192083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com