c - RGB 颜色系统作为数据类型

标签 c

An RGB color system can represent 256 different colors by combining different shades of red, green and blue. Each shade can be represented as a number. Define a C type to represent the RGB state.

根据我的理解,这将是一个合适的 C 类型,但不确定我是否正确:

struct{
    int R[256];
    int G[256];
    int B[256];
} color;

这是正确的还是有更合适/更有效的方法?

最佳答案

您所需要的只是一个可以容纳三个 值的结构,每个值都在0..255 范围内。您建议的结构包含 3*256 * sizeof(int) = 768 * sizeof(int),太多了。改用这个:

struct color {
    unsigned char R;
    unsigned char G;
    unsigned char B;
};

注意结构标识符 color 的正确位置。

您可以像这样在您的程序中进一步使用此结构:

struct color bright_orange;

bright_orange.R = 255;
bright_orange.G = 128;
bright_orange.B = 0;

关于c - RGB 颜色系统作为数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22849623/

相关文章:

c - 返回 char 与 int 变量在 C 中如何工作?

c++ - 在使用小数的简单 C++ 程序中遇到问题。

c - 在C中从http接收XML文件

c++ - 是否存在用于Content Aware Image转换的C/C++开源库?

c - 如何使用UDP套接字在C中发送结构而不进行序列化?

c - 为什么不能在 C 中比较结构的 NULLness?

c - C语言中的字符编码取决于什么?

c - 为什么“while(!feof(file))”总是错误的?

c - Windows Hook 保存语言字符

c - 为什么 realloc 会出错?错误中止(核心转储)