views:

123

answers:

1

I need to create 4 color gradient logic. One of the posts gave me the pattern to calculate 4 color gradient. However I am having difficutlties to implement it.

I am able to calculate color of point E ( middle one) however I do not know how to develop the gradient from it

MY code

COLORREF NewColor = RGB(255,0,0);
 COLORREF NewColor2 = RGB(0,255,0);
 COLORREF NewColor3 = RGB(0,0,255);
 COLORREF NewColor4 = RGB(255,255,0);
 red_low  = GetRValue(NewColor); 
 blue_low = GetBValue(NewColor);  
 green_low = GetGValue(NewColor);
 red_high = GetRValue(NewColor2); 
 green_high= GetGValue(NewColor2);  
 blue_high = GetBValue(NewColor2);

 red_low2 = GetRValue(NewColor3); 
 blue_low2 = GetBValue(NewColor3); // divide the difference in colours by the number of steps 
 green_low2 = GetGValue(NewColor3);
 red_high2 = GetRValue(NewColor4); 
 green_high2 = GetGValue(NewColor4); // divide the difference in colours by the number of steps 
 blue_high2 = GetBValue(NewColor4);        
double  distance_a = sqrt((double)((0-W)^2+(0-H/2)^2));
  double  distance_b = sqrt((double)((350-W/2)^2+(0-H/2)^2));  
  double  distance_c = sqrt((double)((350-W/2)^2+(100-H/2)^2));
  double  distance_d = sqrt((double)((0-W/2)^2+(100-H/2)^2));
  double sum_distances = distance_a + distance_b + distance_c + distance_d;
  double red = (red_low*distance_a + red_high*distance_b + red_low2*distance_c+ red_high2*distance_d) / sum_distances;
  double green = (green_low*distance_a + green_high*distance_b + green_low2*distance_c+ green_high2*distance_d) / sum_distances;
  double blue = (blue_low*distance_a + blue_high*distance_b + blue_low2*distance_c+ blue_high2*distance_d) / sum_distances;
COLORREF Color_E= RGB(red,green,blue);    

Ane help how to futher code it is appreciated.

+2  A: 

Here's code that computes the color at (x,y) given the four corner colors.

#include <vector>
#include <cassert>
#include <windows.h>

using namespace std;

int interpolate(int a, int b, int c, int d, double t, double s)
{
    return (int)(a*(1-t)*(1-s) + b*t*(1-s) + c*(1-t)*s + d*t*s);
}

COLORREF interpolateColors(const vector<COLORREF> &cornercolors, int W, int H, int x, int y)
{
    assert(cornercolors.size() == 4);
    double t = ((double)x)/W;
    double s = ((double)y)/H;
    return RGB( interpolate(GetRValue(cornercolors[0]), GetRValue(cornercolors[1]), GetRValue(cornercolors[2]), GetRValue(cornercolors[3]), t, s),
                interpolate(GetGValue(cornercolors[0]), GetGValue(cornercolors[1]), GetGValue(cornercolors[2]), GetGValue(cornercolors[3]), t, s),
                interpolate(GetBValue(cornercolors[0]), GetBValue(cornercolors[1]), GetBValue(cornercolors[2]), GetBValue(cornercolors[3]), t, s) );
}
Can you tell what headers are required for this to work ?
<vector>, <cassert> (though this one's not necessary, feel free to comment out the assert line), and <windows.h>.
Thanks that works. You guys on this forum are real experts. I was struggling with it for the past 3 days.