0

我编写了一个程序,它可以提供任何 RGB 值,并且可以从程序中找到相似的颜色。但结果有点令人困惑。程序有点长,主要是填写一些RGB数据。主要要点如下

//MyColor.h
#pragma once

#ifndef __MYCOLOR_H__
#define __MYCOLOR_H__

#include <windef.h>
#include <wingdi.h>

#include <vector>


namespace my
{

struct ColorData
{
    UINT8 family;
    UINT16 id;
    const char * name;
    const char * hex;
    UINT8   rgb[3];

    ColorData(UINT8 _family, UINT16 _id, const char *_name, UINT8 _r, UINT8 _g, UINT8 _b, const char * _hex)
    {
        family  = _family;
        id      = _id;
        name    = _name;
        hex     = _hex;
        rgb[0]  = _r;
        rgb[1]  = _g;
        rgb[2]  = _b;
    }
    ColorData(){};
};

/*

    MyColorSpace::myColorFamily cc;
    MyColorSpace::myColorFamily::data *da;

    da = cc.Get(cc.family_blue, cc.blue_deepsky);
    da = cc.Get(cc.family_green, cc.green_yellowGreen);
    da = cc.Get(cc.family_orange, cc.orange_tomato);

    debug(("name,hex,rgb:<%s>,<%s>,[%d,%d,%d]\n",da->name,da->hex,(da->rgb)[0],(da->rgb)[1],(da->rgb)[2]));

*/
class ColorFamily {


public:


    enum family
    {
        family_gray = 0,
        family_red = 1,
        family_orange = 2,
        family_brown = 3,
        family_blue = 4,
        family_green = 5,
        family_pink = 6,
        family_purple = 7,
        family_white = 8,
        family_yellow = 9
    };

    enum gray {
        gray_gainsboro = 0,
        gray_light = 1,
        gray_silver = 2,
        gray_dark = 3,
        gray_ = 4,
        gray_dim = 5,
        gray_lightslate = 6,
        gray_slate = 7,
        gray_darkslate = 8,
        gray_black = 9
    };
    enum red {
        red_indian = 0,
        red_lightcoral = 1,
        red_salmon = 2,
        red_darksalmon = 3,
        red_lightsalmon = 4,
        red_crimson = 5,
        red_ = 6,
        red_firebrick = 7,
        red_dark = 8
    };
    enum orange {
        orange_lightsalmon = 0,
        orange_coral = 1,
        orange_tomato = 2,
        orange_red = 3,
        orange_dark = 4,
        orange_ = 5
    };
    enum brown {
        brown_cornsilk = 0,
        brown_blanchedalmond = 1,
        brown_bisque = 2,
        brown_navajowhite = 3,
        brown_wheat = 4,
        brown_burlywood = 5,
        brown_tan = 6,
        brown_rosy = 7,
        brown_sandy = 8,
        brown_goldenrod = 9,
        brown_darkgoldenrod = 10,
        brown_peru = 11,
        brown_chocolate = 12,
        brown_saddle = 13,
        brown_sienna = 14,
        brown_ = 15,
        brown_maroon = 16
    };
    enum blue {
        blue_aqua = 0,
        blue_cyan = 1,
        blue_lightcyan = 2,
        blue_paleturquoise = 3,
        blue_aquamarine = 4,
        blue_turquoise = 5,
        blue_mediumturquoise = 6,
        blue_darkturquoise = 7,
        blue_cadet = 8,
        blue_steel = 9,
        blue_lightsteel = 10,
        blue_powder = 11,
        blue_light = 12,
        blue_sky = 13,
        blue_lightsky = 14,
        blue_deepsky = 15,
        blue_dodger = 16,
        blue_cornflower = 17,
        blue_mediumslate = 18,
        blue_royal = 19,
        blue_ = 20,
        blue_medium = 21,
        blue_dark = 22,
        blue_navy = 23,
        blue_midnight = 24
    };
    enum green {
        green_yellow = 0,
        green_chartreuse = 1,
        green_lawn = 2,
        green_lime = 3,
        green_limeGreen = 4,
        green_pale = 5,
        green_light = 6,
        green_mediumspring = 7,
        green_spring = 8,
        green_mediumsea = 9,
        green_sea = 10,
        green_forest = 11,
        green_ = 12,
        green_dark = 13,
        green_yellowGreen = 14,
        green_olivedrab = 15,
        green_olive = 16,
        green_darkolive = 17,
        green_mediumaquamarine = 18,
        green_darksea = 19,
        green_lightsea = 20,
        green_darkcyan = 21,
        green_teal = 22
    };
    enum pink {
        pink_ = 0,
        pink_light = 1,
        pink_hot = 2,
        pink_deep = 3,
        pink_mediumvioletred = 4,
        pink_palevioletred = 5
    };
    enum purple {
        purple_lavender = 0,
        purple_thistle = 1,
        purple_plum = 2,
        purple_violet = 3,
        purple_orchid = 4,
        purple_fuchsia = 5,
        purple_magenta = 6,
        purple_mediumorchid = 7,
        purple_medium = 8,
        purple_rebecca = 9,
        purple_blueviolet = 10,
        purple_darkviolet = 11,
        purple_darkorchid = 12,
        purple_darkmagenta = 13,
        purple_ = 14,
        purple_indigo = 15,
        purple_slateblue = 16,
        purple_darkslateblue = 17,
        purple_mediumslateblue = 18
    };
    enum white {
        white_ = 0,
        white_snow = 1,
        white_honeydew = 2,
        white_mintcream = 3,
        white_azure = 4,
        white_aliceblue = 5,
        white_ghost = 6,
        white_smoke = 7,
        white_seashell = 8,
        white_beige = 9,
        white_oldlace = 10,
        white_floral = 11,
        white_ivory = 12,
        white_antique = 13,
        white_linen = 14,
        white_lavenderblush = 15,
        white_mistyrose = 16
    };
    enum yellow {
        yellow_gold = 0,
        yellow_ = 1,
        yellow_light = 2,
        yellow_lemonchiffon = 3,
        yellow_lightgoldenrod = 4,
        yellow_papayawhip = 5,
        yellow_moccasin = 6,
        yellow_peachpuff = 7,
        yellow_palegoldenrod = 8,
        yellow_khaki = 9,
        yellow_darkkhaki = 10
    };
    ColorFamily();
    ~ColorFamily() = default;

private:
    ColorData * _detail;

public:
    std::vector<ColorData>List;

    ColorData* Get(const char * name);
    ColorData* Get(UINT8 _family, UINT16 _id);

    bool Get(UINT8 _r,UINT8 _g,UINT8 _b,ColorData &_out);

    void FillColorData(ColorData &tar,ColorData* src);

};


}


#endif


//MyColor.cpp

#include "MyColor.h"

#include "stdlib.h"

#include "debug.h"

#define Less10(a,b)     abs(a-b) < 10 ? true : false

namespace my
{

    ColorFamily::ColorFamily()
    {
        //data da("",20,30,40,"#hex")
        //List.push_back(data(0,"name", 20, 30, 40, "#hex"));
        //gray
        //gray
        List.push_back(ColorData(0, 0, "gray_gainsboro", 220, 220, 220, "#dcdcdc"));
        List.push_back(ColorData(0, 1, "gray_light", 211, 211, 211, "#d3d3d3"));
        List.push_back(ColorData(0, 2, "gray_silver", 192, 192, 192, "#c0c0c0"));
        List.push_back(ColorData(0, 3, "gray_dark", 169, 169, 169, "#a9a9a9"));
        List.push_back(ColorData(0, 4, "gray_", 128, 128, 128, "#808080"));
        List.push_back(ColorData(0, 5, "gray_dim", 105, 105, 105, "#696969"));
        List.push_back(ColorData(0, 6, "gray_lightslate", 119, 136, 153, "#778899"));
        List.push_back(ColorData(0, 7, "gray_slate", 112, 128, 144, "#708090"));
        List.push_back(ColorData(0, 8, "gray_darkslate", 47, 79, 79, "#2f4f4f"));
        List.push_back(ColorData(0, 9, "gray_black", 0, 0, 0, "#000000"));

        //red
        List.push_back(ColorData(1, 0, "red_indian", 205, 92, 92, "#cd5c5c"));
        List.push_back(ColorData(1, 1, "red_lightcoral", 240, 128, 128, "#f08080"));
        List.push_back(ColorData(1, 2, "red_salmon", 250, 128, 114, "#fa8072"));
        List.push_back(ColorData(1, 3, "red_darksalmon", 233, 150, 122, "#e9967a"));
        List.push_back(ColorData(1, 4, "red_lightsalmon", 255, 160, 122, "#ffa07a"));
        List.push_back(ColorData(1, 5, "red_crimson", 220, 20, 60, "#dc143c"));
        List.push_back(ColorData(1, 6, "red_", 255, 0, 0, "#ff0000"));
        List.push_back(ColorData(1, 7, "red_firebrick", 178, 34, 34, "#b22222"));
        List.push_back(ColorData(1, 8, "red_dark", 139, 0, 0, "#8b0000"));

        //orange
        List.push_back(ColorData(2, 0, "orange_lightsalmon", 255, 160, 122, "#ffa07a"));
        List.push_back(ColorData(2, 1, "orange_coral", 255, 127, 80, "#ff7f50"));
        List.push_back(ColorData(2, 2, "orange_tomato", 255, 99, 71, "#ff6347"));
        List.push_back(ColorData(2, 3, "orange_red", 255, 69, 0, "#ff4500"));
        List.push_back(ColorData(2, 4, "orange_dark", 255, 140, 0, "#ff8c00"));
        List.push_back(ColorData(2, 5, "orange_", 255, 165, 0, "#ffa500"));

        //brown
        List.push_back(ColorData(3, 0, "brown_cornsilk", 255, 248, 220, "#fff8dc"));
        List.push_back(ColorData(3, 1, "brown_blanchedalmond", 255, 235, 205, "#ffebcd"));
        List.push_back(ColorData(3, 2, "brown_bisque", 255, 228, 196, "#ffe4c4"));
        List.push_back(ColorData(3, 3, "brown_navajowhite", 255, 222, 173, "#ffdead"));
        List.push_back(ColorData(3, 4, "brown_wheat", 245, 222, 179, "#f5deb3"));
        List.push_back(ColorData(3, 5, "brown_burlywood", 222, 184, 135, "#deb887"));
        List.push_back(ColorData(3, 6, "brown_tan", 210, 180, 140, "#d2b48c"));
        List.push_back(ColorData(3, 7, "brown_rosy", 188, 143, 143, "#bc8f8f"));
        List.push_back(ColorData(3, 8, "brown_sandy", 244, 164, 96, "#f4a460"));
        List.push_back(ColorData(3, 9, "brown_goldenrod", 218, 165, 32, "#daa520"));
        List.push_back(ColorData(3, 10, "brown_darkgoldenrod", 184, 134, 11, "#b8860b"));
        List.push_back(ColorData(3, 11, "brown_peru", 205, 133, 63, "#cd853f"));
        List.push_back(ColorData(3, 12, "brown_chocolate", 210, 105, 30, "#d2691e"));
        List.push_back(ColorData(3, 13, "brown_saddle", 139, 69, 19, "#8b4513"));
        List.push_back(ColorData(3, 14, "brown_sienna", 160, 82, 45, "#a0522d"));
        List.push_back(ColorData(3, 15, "brown_", 165, 42, 42, "#a52a2a"));
        List.push_back(ColorData(3, 16, "brown_maroon", 128, 0, 0, "#800000"));

        //blue
        List.push_back(ColorData(4, 0, "blue_aqua", 0, 255, 255, "#00ffff"));
        List.push_back(ColorData(4, 1, "blue_cyan", 0, 255, 255, "#00ffff"));
        List.push_back(ColorData(4, 2, "blue_lightcyan", 224, 255, 255, "#e0ffff"));
        List.push_back(ColorData(4, 3, "blue_paleturquoise", 175, 238, 238, "#afeeee"));
        List.push_back(ColorData(4, 4, "blue_aquamarine", 127, 255, 212, "#7fffd4"));
        List.push_back(ColorData(4, 5, "blue_turquoise", 64, 224, 208, "#40e0d0"));
        List.push_back(ColorData(4, 6, "blue_mediumturquoise", 72, 209, 204, "#48d1cc"));
        List.push_back(ColorData(4, 7, "blue_darkturquoise", 0, 206, 209, "#00ced1"));
        List.push_back(ColorData(4, 8, "blue_cadet", 95, 158, 160, "#5f9ea0"));
        List.push_back(ColorData(4, 9, "blue_steel", 70, 130, 180, "#4682b4"));
        List.push_back(ColorData(4, 10, "blue_lightsteel", 176, 196, 222, "#b0c4de"));
        List.push_back(ColorData(4, 11, "blue_powder", 176, 224, 230, "#b0e0e6"));
        List.push_back(ColorData(4, 12, "blue_light", 173, 216, 230, "#add8e6"));
        List.push_back(ColorData(4, 13, "blue_sky", 135, 206, 235, "#87ceeb"));
        List.push_back(ColorData(4, 14, "blue_lightsky", 135, 206, 250, "#87cefa"));
        List.push_back(ColorData(4, 15, "blue_deepsky", 0, 191, 255, "#00bfff"));
        List.push_back(ColorData(4, 16, "blue_dodger", 30, 144, 255, "#1e90ff"));
        List.push_back(ColorData(4, 17, "blue_cornflower", 100, 149, 237, "#6495ed"));
        List.push_back(ColorData(4, 18, "blue_mediumslate", 123, 104, 238, "#7b68ee"));
        List.push_back(ColorData(4, 19, "blue_royal", 65, 105, 225, "#4169e1"));
        List.push_back(ColorData(4, 20, "blue_", 0, 0, 255, "#0000ff"));
        List.push_back(ColorData(4, 21, "blue_medium", 0, 0, 205, "#0000cd"));
        List.push_back(ColorData(4, 22, "blue_dark", 0, 0, 139, "#00008b"));
        List.push_back(ColorData(4, 23, "blue_navy", 0, 0, 128, "#000080"));
        List.push_back(ColorData(4, 24, "blue_midnight", 25, 25, 112, "#191970"));

        //green
        List.push_back(ColorData(5, 0, "green_yellow", 173, 255, 47, "#adff2f"));
        List.push_back(ColorData(5, 1, "green_chartreuse", 127, 255, 0, "#7fff00"));
        List.push_back(ColorData(5, 2, "green_lawn", 124, 252, 0, "#7cfc00"));
        List.push_back(ColorData(5, 3, "green_lime", 0, 255, 0, "#00ff00"));
        List.push_back(ColorData(5, 4, "green_lime", 50, 205, 50, "#32cd32"));
        List.push_back(ColorData(5, 5, "green_pale", 152, 251, 152, "#98fb98"));
        List.push_back(ColorData(5, 6, "green_light", 144, 238, 144, "#90ee90"));
        List.push_back(ColorData(5, 7, "green_mediumspring", 0, 250, 154, "#00fa9a"));
        List.push_back(ColorData(5, 8, "green_spring", 0, 255, 127, "#00ff7f"));
        List.push_back(ColorData(5, 9, "green_mediumsea", 60, 179, 113, "#3cb371"));
        List.push_back(ColorData(5, 10, "green_sea", 46, 139, 87, "#2e8b57"));
        List.push_back(ColorData(5, 11, "green_forest", 34, 139, 34, "#228b22"));
        List.push_back(ColorData(5, 12, "green_", 0, 128, 0, "#008000"));
        List.push_back(ColorData(5, 13, "green_dark", 0, 100, 0, "#006400"));
        List.push_back(ColorData(5, 14, "green_yellow", 154, 205, 50, "#9acd32"));
        List.push_back(ColorData(5, 15, "green_olivedrab", 107, 142, 35, "#6b8e23"));
        List.push_back(ColorData(5, 16, "green_olive", 128, 128, 0, "#808000"));
        List.push_back(ColorData(5, 17, "green_darkolive", 85, 107, 47, "#556b2f"));
        List.push_back(ColorData(5, 18, "green_mediumaquamarine", 102, 205, 170, "#66cdaa"));
        List.push_back(ColorData(5, 19, "green_darksea", 143, 188, 139, "#8fbc8b"));
        List.push_back(ColorData(5, 20, "green_lightsea", 32, 178, 170, "#20b2aa"));
        List.push_back(ColorData(5, 21, "green_darkcyan", 0, 139, 139, "#008b8b"));
        List.push_back(ColorData(5, 22, "green_teal", 0, 128, 128, "#008080"));

        //pink
        List.push_back(ColorData(6, 0, "pink_", 255, 192, 203, "#ffc0cb"));
        List.push_back(ColorData(6, 1, "pink_light", 255, 182, 193, "#ffb6c1"));
        List.push_back(ColorData(6, 2, "pink_hot", 255, 105, 180, "#ff69b4"));
        List.push_back(ColorData(6, 3, "pink_deep", 255, 20, 147, "#ff1493"));
        List.push_back(ColorData(6, 4, "pink_mediumvioletred", 199, 21, 133, "#c71585"));
        List.push_back(ColorData(6, 5, "pink_palevioletred", 219, 112, 147, "#db7093"));

        //purple
        List.push_back(ColorData(7, 0, "purple_lavender", 230, 230, 250, "#e6e6fa"));
        List.push_back(ColorData(7, 1, "purple_thistle", 216, 191, 216, "#d8bfd8"));
        List.push_back(ColorData(7, 2, "purple_plum", 221, 160, 221, "#dda0dd"));
        List.push_back(ColorData(7, 3, "purple_violet", 238, 130, 238, "#ee82ee"));
        List.push_back(ColorData(7, 4, "purple_orchid", 218, 112, 214, "#da70d6"));
        List.push_back(ColorData(7, 5, "purple_fuchsia", 255, 0, 255, "#ff00ff"));
        List.push_back(ColorData(7, 6, "purple_magenta", 255, 0, 255, "#ff00ff"));
        List.push_back(ColorData(7, 7, "purple_mediumorchid", 186, 85, 211, "#ba55d3"));
        List.push_back(ColorData(7, 8, "purple_medium", 147, 112, 219, "#9370db"));
        List.push_back(ColorData(7, 9, "purple_rebecca", 102, 51, 153, "#663399"));
        List.push_back(ColorData(7, 10, "purple_blueviolet", 138, 43, 226, "#8a2be2"));
        List.push_back(ColorData(7, 11, "purple_darkviolet", 148, 0, 211, "#9400d3"));
        List.push_back(ColorData(7, 12, "purple_darkorchid", 153, 50, 204, "#9932cc"));
        List.push_back(ColorData(7, 13, "purple_darkmagenta", 139, 0, 139, "#8b008b"));
        List.push_back(ColorData(7, 14, "purple_", 128, 0, 128, "#800080"));
        List.push_back(ColorData(7, 15, "purple_indigo", 75, 0, 130, "#4b0082"));
        List.push_back(ColorData(7, 16, "purple_slateblue", 106, 90, 205, "#6a5acd"));
        List.push_back(ColorData(7, 17, "purple_darkslateblue", 72, 61, 139, "#483d8b"));
        List.push_back(ColorData(7, 18, "purple_mediumslateblue", 123, 104, 238, "#7b68ee"));

        //white
        List.push_back(ColorData(8, 0, "white_", 255, 255, 255, "#ffffff"));
        List.push_back(ColorData(8, 1, "white_snow", 255, 250, 250, "#fffafa"));
        List.push_back(ColorData(8, 2, "white_honeydew", 240, 255, 240, "#f0fff0"));
        List.push_back(ColorData(8, 3, "white_mintcream", 245, 255, 250, "#f5fffa"));
        List.push_back(ColorData(8, 4, "white_azure", 240, 255, 255, "#f0ffff"));
        List.push_back(ColorData(8, 5, "white_aliceblue", 240, 248, 255, "#f0f8ff"));
        List.push_back(ColorData(8, 6, "white_ghost", 248, 248, 255, "#f8f8ff"));
        List.push_back(ColorData(8, 7, "white_smoke", 245, 245, 245, "#f5f5f5"));
        List.push_back(ColorData(8, 8, "white_seashell", 255, 245, 238, "#fff5ee"));
        List.push_back(ColorData(8, 9, "white_beige", 245, 245, 220, "#f5f5dc"));
        List.push_back(ColorData(8, 10, "white_oldlace", 253, 245, 230, "#fdf5e6"));
        List.push_back(ColorData(8, 11, "white_floral", 255, 250, 240, "#fffaf0"));
        List.push_back(ColorData(8, 12, "white_ivory", 255, 255, 240, "#fffff0"));
        List.push_back(ColorData(8, 13, "white_antique", 250, 235, 215, "#faebd7"));
        List.push_back(ColorData(8, 14, "white_linen", 250, 240, 230, "#faf0e6"));
        List.push_back(ColorData(8, 15, "white_lavenderblush", 255, 240, 245, "#fff0f5"));
        List.push_back(ColorData(8, 16, "white_mistyrose", 255, 228, 225, "#ffe4e1"));

        //yellow
        List.push_back(ColorData(9, 0, "yellow_gold", 255, 215, 0, "#ffd700"));
        List.push_back(ColorData(9, 1, "yellow_", 255, 255, 0, "#ffff00"));
        List.push_back(ColorData(9, 2, "yellow_light", 255, 255, 224, "#ffffe0"));
        List.push_back(ColorData(9, 3, "yellow_lemonchiffon", 255, 250, 205, "#fffacd"));
        List.push_back(ColorData(9, 4, "yellow_lightgoldenrod", 250, 250, 210, "#fafad2"));
        List.push_back(ColorData(9, 5, "yellow_papayawhip", 255, 239, 213, "#ffefd5"));
        List.push_back(ColorData(9, 6, "yellow_moccasin", 255, 228, 181, "#ffe4b5"));
        List.push_back(ColorData(9, 7, "yellow_peachpuff", 255, 218, 185, "#ffdab9"));
        List.push_back(ColorData(9, 8, "yellow_palegoldenrod", 238, 232, 170, "#eee8aa"));
        List.push_back(ColorData(9, 9, "yellow_khaki", 240, 230, 140, "#f0e68c"));
        List.push_back(ColorData(9, 10, "yellow_darkkhaki", 189, 183, 107, "#bdb76b"));
    };


    void ColorFamily::FillColorData(ColorData &tar,ColorData* src)
    {
        tar.family  = src->family;
        tar.hex     = src->hex;
        tar.id      = src->id;
        tar.name    = src->name;
        tar.rgb[0]  = src->rgb[0];
        tar.rgb[1]  = src->rgb[1];
        tar.rgb[2]  = src->rgb[2];
    }


    ColorData* ColorFamily::Get(const char * name)
    {
        for (int i = 0; i < List.size(); i++)
        {
            ColorData da = List[i];
            if (strcmp(da.name, name))
            {
                _detail->family = da.family;
                _detail->hex = da.hex;
                _detail->id = da.id;
                _detail->name = da.name;
                _detail->rgb[0] = da.rgb[0];
                _detail->rgb[1] = da.rgb[1];
                _detail->rgb[2] = da.rgb[2];
                return _detail;
            }
        }
    }

    ColorData* ColorFamily::Get(UINT8 _family, UINT16 _id)
    {
        for (int i = 0; i < List.size(); i++)
        {
            _detail = &List[i];

            if (_detail->family == _family && _detail->id == _id)
            {
                return _detail;
            }
        }
    }

    bool ColorFamily::Get(UINT8 _r,UINT8 _g,UINT8 _b,ColorData &_out)
    {

        debug("rgb(%d,%d,%d)\n", _r,_g,_b);
        struct similar
        {
            UINT8 dt;
            UINT8 family;
            UINT8 color;
            similar(UINT8 _dt, UINT8 _family, UINT8 _color){
                dt      = _dt;
                family  = _family;
                color   = _color;
            }
        };

        std::vector<similar> sList;

        for (int i = 0; i < List.size(); i++)
        {
            _detail = &List[i];
    
            if (_detail->rgb[0] == _r && _detail->rgb[1] == _g && _detail->rgb[2] == _b)
            {
                FillColorData(_out,_detail);
                return true;
            }
        }


        for (int i = 0; i < List.size(); i++)
        {
            _detail = &List[i];

            unsigned short sum = 0;

            if (Less10(_detail->rgb[0],_r) && 
                Less10(_detail->rgb[1],_g) && 
                Less10(_detail->rgb[2],_b))
            {
                sum += abs(_detail->rgb[0] - _r);
                sum += abs(_detail->rgb[1] - _g);
                sum += abs(_detail->rgb[2] - _b);
                debug("dc:%d,list(%d,%d,%d)\n",sum, _detail->rgb[0],_detail->rgb[1],_detail->rgb[2]);
                sList.push_back(similar(sum,_detail->family,_detail->id));

            }
        }

        unsigned short dt_min_idx = 0;

        if(sList.size() > 0)
        {
            for (int i = 0; i < sList.size(); i++)
            {
                if(sList[dt_min_idx].dt >= sList[i].dt)
                {
                    dt_min_idx = i;
                }
            }

            UINT8 fid = sList[dt_min_idx].family;
            UINT8 cid = sList[dt_min_idx].color;

            FillColorData(_out,Get(fid,cid));

            return true;
        }

        return false;

    }

}
//main.cpp

#include <iostream>
#include <windows.h>

#include "MyColor.h"

#include "debug.h"

using namespace std;

my::bug d;

my::ColorFamily col;

int main()
{

    my::ColorData cd;

    UINT8 rgb[3] = {62,138,120};

    bool rst = col.Get(rgb[0],rgb[1],rgb[2],cd);

    if(rst)
        debug("rst:%d \n rgb(%d,%d,%d)=>rgb(%d,%d,%d) \nfamily:%d,color:%d,name:%s\n",rst,rgb[0],rgb[1],rgb[2],cd.rgb[0],cd.rgb[1],cd.rgb[2],cd.family,cd.id, cd.name);
    else
        debug("no result\n");

    system("pause");
    return 0;

}

//ctrl + shift + p


结果是:

rgb(62,138,120)
dc:176,list(64,224,208)
dc:76,list(70,130,180)
dc:141,list(65,105,225)
dc:50,list(60,179,113)
rst:1 

该程序的结果大致符合我的预期,但其中有一些非常令人困惑的地方。

例如,这里我明确指定了三个条件必须同时为真,然后才将其添加到列表中。

if (Less10(_detail->rgb[0],_r) && 
    Less10(_detail->rgb[1],_g) && 
    Less10(_detail->rgb[2],_b))
{
...
}

但从输出来看,似乎程序只比较了第一个条件。

debug init done
rgb(62,138,120)
dc:176,list(64,224,208)
dc:76,list(70,130,180)
dc:141,list(65,105,225)
dc:50,list(60,179,113)
################################################
rst:1
rgb(62,138,120)=>rgb(60,179,113)
family:5,color:9,name:green_mediumsea
4

1 回答 1

6

您对宏的使用引起了混乱。您定义Less10如下:

#define Less10(a,b)     abs(a-b) < 10 ? true : false

因此,让我们扩展您的条件:

abs(_detail->rgb[0]-_r) < 10 ? true : false && 
abs(_detail->rgb[1]-_g) < 10 ? true : false && 
abs(_detail->rgb[2]-_b) < 10 ? true : false)

由于 : 的运算符优先级很低,因此解析如下:

abs(_detail->rgb[0]-_r) < 10 ? true :
  (false && abs(_detail->rgb[1]-_g) < 10 ? true :
    (false && abs(_detail->rgb[2]-_b) < 10 ? true : false)))

因为false && EXPR === false,它归结为:

abs(_detail->rgb[0]-_r) < 10 ? true : false

要修复它,要么制作一个真正的函数,要么将宏定义包装在括号中,并在你使用它时Less10摆脱无关紧要的东西(因为条件已经是真或假)? true : false

#define Less10(a,b)     (abs(a-b) < 10)
于 2021-06-23T07:57:40.600 回答