2010-06-04 65 views
0

我需要创建4个颜色渐变逻辑。其中一个帖子给了我计算4种颜色梯度的模式。但是我有差异来实现它。4颜色渐变逻辑需要

我能计算出E点的颜色(中间的一个),但我不知道如何开发梯度从中

我的代码

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);  

安逸帮助如何futher码是赞赏。

+0

你想做什么?插入矩形的颜色? NewColori是角落里的颜色吗? – user168715 2010-06-04 19:45:12

+0

是的,我想创建一个方形或四角有四种颜色的矩形。屏幕上的每个像素应显示这四种颜色的适当组合。我需要使用SetPixel或使用memcpy复制到frambuffer。新颜色是角落处的颜色。 – user358791 2010-06-04 19:53:00

+1

您*需要使用''中的LinearGradientBrush来做到这一点。没有重新发明那个轮子的意义。 – 2010-06-04 20:16:40

回答

3

下面是计算给定四个角落颜色时(x,y)颜色的代码。

#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)); 
} 
+0

你能说出这个工作需要什么标题吗? – user358791 2010-06-04 20:11:57

+0

,(虽然这不是必要的,可以随意注释断言行),并且。 – user168715 2010-06-04 20:13:42

+0

谢谢你的作品。你们在这个论坛上是真正的专家。过去3天我一直在努力。 – user358791 2010-06-04 20:45:55