-7

嘿,你怎么样?我想问一下 C 语言中是否有任何方法可以在不存储在数组或文件中的情况下打印颜色名称.....就像我们放置特定颜色的代码,程序将打印白色,例如 #FFFFFFF 或类似的东西??我想在我的项目中使用大量颜色名称

4

2 回答 2

3

可以计算出某物是什么颜色——例如,以红色+蓝色为主的东西变成“紫色”,很多“红色”,而其他颜色不多的是“红色”,如果它是高水平它是“浅色”,如果水平是“低”天黑了。但是仍然有大量的颜色组合,并不是你可以从 HTML 颜色组合的 16777216 种颜色中的每一种都可以称为名称。

如果我们称 808080 为“中灰色”,那么几乎相同的 818181 和 7f7f7f 怎么称呼?

(并且“不使用数组”,这可能使这几乎不可能 - 当然,我们可以制作“代码表”而不是“数组”,例如使用开关或长链 if 语句,但如果你不在这个过程中的计算,你只是让编译器为你构建一个表,而不是首先使用一个数组)

这是一些命名颜色的代码 - 它远非完美,但它可能会给你一些想法......我试图实现一些具有一定灵活性的东西。还有其他方法可以实现类似的目标。但这是一个快速的尝试,我敢肯定,如果我再花几个小时在上面,并且有一些有意义的输入数据[我缺乏想出颜色的想象力......]

#include <stdio.h>
#include <string.h>

typedef struct tColourDef
{
    int r;
    int g;
    int b;
    const char *colour;
} ColourDef;


// Portion up the colours in 1/8 of the range, 0-7. 

static ColourDef baseColours[] = 
{
    { 0, 0, 0, "Unknown" },
    { 0, 0, 0, "Black" },
    { 7, 7, 7, "White" },
    { 7, 0, 0, "Red" },
    { 0, 7, 0, "Green" },
    { 0, 0, 7, "Blue" },
    { 7, 7, 0, "Yellow" },
    { 4, 4, 4, "Gray" },
    { 7, 0, 4, "Pink" },
    { 5, 2, 0, "Brown" },
    { 7, 0, 5, "Magenta" },
    { 0, 7, 7, "Cyan" },
    { 4, 0, 4, "Purple" },
    { 7, 3, 0, "Orange" },
    { 4, 0, 0, "Maroon" },
    { 5, 0, 7, "Violet" },
    { 2, 6, 6, "Turqoise" },
};

#define NCOLOURS (sizeof baseColours/sizeof baseColours[0])

inline int iabs(int x)
{
    if (x < 0) return -x;
    return x;
}

int FindColour(int r, int g, int b, char *buffer, size_t buffer_size)
{
    int i;
    // Shift colour down... 
    r >>= 5;
    g >>= 5;
    b >>= 5;
    int smallestError = 5;   // Bigger than this, and we say "unknown".
    int bestIndex = 0;       // Point at "unknown"
    for(i = 1; i < NCOLOURS; i++)
    {
        int error;
        error = abs(r - baseColours[i].r) +
            abs(b - baseColours[i].b) + 
            abs(g - baseColours[i].g);
        if (error < smallestError && error < 3)
        {
            smallestError = error;
            bestIndex = i;
        }
    }
    strncpy(buffer, baseColours[bestIndex].colour, buffer_size);
    return (bestIndex > 0);
}


int main()
{
    int testColours[] = { 0xFF0000, 
                          0x00FF00, 
                          0x0000FF, 
                          0x008080, 
                          0x808080,
                          0x770000,
                          0xf01780,
                          0x333333,
    };
    int i; 
    for(i = 0; i < sizeof testColours / sizeof testColours[0]; i++)
    {
        int r = testColours[i] >> 16;
        int g = (testColours[i] >> 8) & 0xff;
        int b = testColours[i] & 0xff;
        char buffer[30];
        int res = FindColour(r, g, b, buffer, sizeof(buffer));
        printf("%02x:%02x:%02x = %s\n", r, g, b, buffer);
    }
    return 0;
}
于 2013-09-08T19:47:56.883 回答
1

使用X 宏

#include <stdlib.h>
#include <stdio.h>

#define COLORS_RGB \
  COLOR_ENTRY(white, 0xffffff) \
  COLOR_ENTRY(black, 0x000000) \
  COLOR_ENTRY(red, 0xff0000) \
  COLOR_ENTRY(green, 0x00ff00) \
  COLOR_ENTRY(blue, 0x0000ff)

#define STR(x) #x

char* get_color_name_rgb(unsigned int color) {
    switch(color) {
#define COLOR_ENTRY(name,value) case value: return STR(name);
    COLORS_RGB
#undef COLOR_ENTRY
    default: return "unknown color";
    };
}

int main ()
{
    unsigned int colors[] = {0xffffff, 0xff0000, 0x00ff00,
                             0x0000ff, 0x000000, 0x123456};
    size_t ncolors = sizeof(colors)/sizeof(*colors);
    int i;
    for (i = 0; i < ncolors; i++)
        printf("value: 0x%06x, name: %s\n",
               colors[i], get_color_name_rgb(colors[i]));
}

输出是:

$ gcc test.c && ./a.out
value: 0xffffff, name: white
value: 0xff0000, name: red
value: 0x00ff00, name: green
value: 0x0000ff, name: blue
value: 0x000000, name: black
value: 0x123456, name: unknown color
于 2013-09-08T19:29:59.597 回答