[英]How to generate 24-bit RGB values “more gracefully”?
Source file: 源文件:
/* gcc <filename.c> -o <filename> -std=c99 -Wall */
#include <stdio.h>
int main (void)
{
unsigned int RGB[8];
for (unsigned char Cnt = 1; Cnt < 8; Cnt++)
{
RGB[Cnt] = ((unsigned int)((6+Cnt)/7)) * (((unsigned int) 0xFF) << (8*((9-Cnt)%3)))
+ ((unsigned int) (Cnt/4)) * (((unsigned int) 0xFF) << (8*((8-Cnt)%3)))
+ ((unsigned int) (Cnt/7)) * ((unsigned int) 0xFF);
fprintf (stdout, "%06X\n", RGB[Cnt]);
}
}
Output: (order: R -> G -> B -> R+G -> G+B -> B+R -> R+G+B) 输出:(顺序:R - > G - > B - > R + G - > G + B - > B + R - > R + G + B)
FF0000
FF0000
00FF00
00FF00
0000FF
0000FF
FFFF00
FFFF00
00FFFF
00FFFF
FF00FF
FF00FF
FFFFFF
FFFFFF
I wonder... 我想知道...
whether there are any more graceful ways to generate 24-bit RGB values in this order as above? 是否有更优雅的方法按上述顺序生成24位RGB值?
whether those type-castings are necessary in ISO C99 ? ISO C99中是否需要这些铸件?
whether unsigned symbols in type-casting are necessary in ISO C99 in this case? 在这种情况下,ISO C99中是否需要使用类型转换中的无符号符号?
Thanks. 谢谢。
Simple way is just to hardcode values, it will be easy to read. 简单的方法就是硬编码值,它很容易阅读。
Anyway if you want to generate them - this variant is faster neither integer division: 无论如何,如果你想生成它们 - 这个变体既不快整数除法:
unsigned int RGB[7];
unsigned int values[] = {0, 0xff};
unsigned int rgbmask = 0x1EBC54; // binary 111101011110001010100
for (int i=0; i<7; i++)
{
RGB[i] = values[rgbmask>>2&1]<<16 | values[rgbmask>>1&1]<<8 | values[rgbmask&1];
rgbmask >>= 3;
printf ("%06X\n", RGB[i]);
}
Possibly not what you wanted to see, but this seems so much better: 可能不是你想看的东西,但这看起来好多了:
unsigned int RGB[8] = {
0x000000,
0xFF0000,
0x00FF00,
0x0000FF,
0xFFFF00,
0x00FFFF,
0xFF00FF,
0xFFFFFF
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.