c - 如何在没有文件或数组的情况下在 C 中打印颜色名称?

标签 c

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

最佳答案

可以计算某物的颜色 - 例如红色+蓝色占主导地位的东西会变成“紫色”,很多“红色”,而其他东西则没有太多“红色”,如果它的水平高,它就是“浅”,如果水平“低”,它就是暗的。但颜色组合的数量仍然非常多,而且并不是从 HTML 颜色中组合的 16777216 种颜色中的每一种都可以称为名称。

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

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

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

#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;
}

关于c - 如何在没有文件或数组的情况下在 C 中打印颜色名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18687502/

相关文章:

带有新 OS X Yosemite 10.10 的 Google Chrome 上的取消按钮消失了

c - 当文件实际存在时 open() 文件不存在

c - 为什么这个销毁函数在单链表中抛出段错误

c - 如何用 ALSA 播放短音

c - 如何在压缩前获取文件的类型

c - 确定以像素为单位的棋盘尺寸

c - 将 makedepend 替换为 cc -MM

c - IEEE 754 : sqrtf() with fesetround(): different results between compilers: 0x42440a72 vs. 0x42440a73

c++ - 线程之间共享内存

c - 如何让我的脚本循环并根据命令退出?