CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: match color

  1. #1
    Guest

    match color

    If I have known the RGB value of some basic colors and another specific color, how to judge which basic color the specific color is most visually similar to? Hope your help.


  2. #2
    Join Date
    May 1999
    Posts
    6

    Re: match color

    The simplistic approach would be to use the distance formula. The Distance value below can be used to indicate how large of a distance between two color coordinates (or points in space). The algorithm follows:

    Given

    RGB(r1,g1,b1)
    RGB(r2,g2,b2)

    Distance = squareroot( square(r2 - r1) + square(g2 - g1) + square(b2 - b1) )

    I am not sure how effective this method would be for computing "color distance". For example, human vision is more sensitive to green than to other hues, but in theory it should work fairly well.


  3. #3
    Guest

    Re: match color

    Thank your help. I had tried the method you mentioned before I posted the question. But the result is incorrect because the visual effect is not proportional to RGB value. For example, the color(128,255,255) is more similar to white(255,255,255) than cyan(0,255,255) according to calculation, actually, this color is almost same as cyan visually.


  4. #4
    Join Date
    May 1999
    Posts
    6

    Re: match color

    You are right! After thinking it over I realized that what you should really use is a different color model to make your color comparisons. If you have already solved this problem, please disregard the rest of this...

    I wrote a small test program that converts input RGB values into HSV (HSB). It then uses the hue, saturation and brightness values to compare the colors. Hue proximity should be weighted more heavily, followed by saturation, and finally brightness. The algorithm is not complete, but it may serve as a starting point if nothing else.
    The code to convert from RGB to HSV follows:

    #define MIN(a,b) ( (a > b)? b : a )
    #define MAX(a,b) ( (a > b)? a : b )

    typedef struct
    {
    double red;
    double green;
    double blue;
    } RGB_DATA;

    typedef struct
    {
    double hue; /* Angle in degrees about the HSV hexcone. */
    double saturation; /* from 0.0 to 1.0 (0 percent to 100 percent) */
    double value; /* from 0.0 to 1.0 (sometimes called brightness) */
    } HSV_DATA;



    /*
    *-----------------------------------------------------------------------
    *
    * FUNCTION: RGB_to_HSV
    *
    * This function converts an RGB color to an HSV color.
    *
    * The color model conversion algorithms are based on the
    * algorithms published in Computer Graphics: Principles and Practice -
    * Second Edition in C, by James D. Foley, Andries van Dam, Steven K. Feiner,
    * and John F. Hughes, published by Addison-Wesley Publishing Company
    * (Reprinted with corrections November 1992, November 1993, and July 1995),
    * Copyright 1996, 1990 by Addison-Wesley Publishing Company, Inc.
    *
    *-----------------------------------------------------------------------
    */
    HSV_DATA CColormatchDlg::RGB_to_HSV(RGB_DATA rgb_in)
    {
    HSV_DATA hsv_out;

    double max = MAX(rgb_in.red, MAX(rgb_in.green, rgb_in.blue));
    double min = MIN(rgb_in.red, MIN(rgb_in.green, rgb_in.blue));
    double delta = max - min;

    hsv_out.value = max;

    if (max==min) /* red, green, and blue were all identical */
    {
    /*
    * This is the achromatic case.
    */
    hsv_out.saturation = 0.0;
    hsv_out.hue = 0.0;
    }
    else
    {
    hsv_out.saturation = delta / max;

    if (rgb_in.red == max)
    {
    hsv_out.hue = (rgb_in.green - rgb_in.blue) / delta;
    }
    else if (rgb_in.green == max)
    {
    hsv_out.hue = 2.0 + (rgb_in.blue - rgb_in.red) / delta;
    }
    else /* (rgb_in.blue == max) */
    {
    hsv_out.hue = 4.0 + (rgb_in.red - rgb_in.green) / delta;
    }

    hsv_out.hue *= 60.0;

    if (hsv_out.hue < 0.0)
    hsv_out.hue += 360.0;
    }

    return (hsv_out);
    }




  5. #5
    Guest

    Re: match color

    can you use getnearestcolor(COLORREF) ?



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured