将8位颜色转换为24位颜色

发布于 2025-02-14 01:08:48 字数 127 浏览 0 评论 0原文

我想知道是否可以将8位RGB颜色(1个字节=单色)转换为24位RGB颜色((8位=红色,8位=绿色,8位=蓝色)=单颜色)。有什么办法可以做到吗? (我不介意只有256种颜色)。另外,如果可能的话,可以将其写成Python中的公式/功能吗?

I was wondering if I can convert an 8-bit RGB color (1 byte = single color) into a 24-bit RGB color ((8 bits = red, 8 bits = green, 8 bits = blue) = single color). Is there a way it can be done? (I don't mind having only 256 colors). Also, if it would be possible can it be written as a formula/function in Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2025-02-21 01:08:48
def convert_8_to_24(byte):
    byte = int(byte, 16)
    red = (byte >> 5) * 32
    green = ((byte & 28) >> 2) * 32
    blue = (byte & 3) * 64
    return (red, green, blue)

def convert_24_to_8(red, green, blue):
    byte = (floor((red / 32)) << 5) + (floor((green / 32)) << 2) + floor((blue / 64))
    byte = hex(byte)[2:]
    if len(byte) == 1:
        byte = "0" + byte
    return byte
def convert_8_to_24(byte):
    byte = int(byte, 16)
    red = (byte >> 5) * 32
    green = ((byte & 28) >> 2) * 32
    blue = (byte & 3) * 64
    return (red, green, blue)

def convert_24_to_8(red, green, blue):
    byte = (floor((red / 32)) << 5) + (floor((green / 32)) << 2) + floor((blue / 64))
    byte = hex(byte)[2:]
    if len(byte) == 1:
        byte = "0" + byte
    return byte
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文