gradient

gradient.h - 颜色渐变

http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients

class ColorGradient{
   private:
    	struct ColorPoint{	// 内部类,用于存储渐变中的不同颜色
            float r, g, b;	// Red, green and blue values of our color.
            float val;	// Position of our color along the gradient (between 0 and 1).
            ColorPoint(float red, float green, float blue, float value):r(red), g(green), b(blue), val(value){}
		};
    	vector<ColorPoint> color; // 升序存储 An array of color points in ascending value.
    public:
    	//-- Default constructor:
    	ColorGradient(){createDefaultHeatMapGradient();}
    	
    	//-- Inserts a new color point into its correct position:
  		//value值从大到小有序,根据value的大小插入对应位置
  		void addColorPoint(float red, float green, float blue, float value) {
    		for (unsigned int i = 0; i < color.size(); i++)  {
      			if (value < color[i].val) {
        		color.insert(color.begin() + i, ColorPoint(red, green, blue, value));
        		return;
      			}
    		}
    		//如果在i位置找不到插入点,即将它插入尾部
    		color.push_back(ColorPoint(red, green, blue, value));
  		}
    
    	//-- Inserts a new color point into its correct position:
  		// 将color里保存的所有颜色清除
  		void clearGradient() { color.clear(); }
    
    	//-- Places a 5 color heapmap gradient into the "color" vector:
  		// 这里直接生成5种颜色表,放入vector中
  		void createDefaultHeatMapGradient() {
    		color.clear();
    		color.push_back(ColorPoint(0, 0, 1,   0.0f));      // Blue.
    		color.push_back(ColorPoint(0, 1, 1,   0.25f));     // Cyan.
    		color.push_back(ColorPoint(0, 1, 0,   0.5f));      // Green.
    		color.push_back(ColorPoint(1, 1, 0,   0.75f));     // Yellow.
    		color.push_back(ColorPoint(1, 0, 0,   1.0f));      // Red.
  		}
    
    	//-- Inputs a (value) between 0 and 1 and outputs the (red), (green) and (blue)
  		//-- values representing that position in the gradient.
  		// 依据value 在[0, 1]之间,输出相应的颜色值RGB
  		void getColorAtValue(const float value, float& red, float& green, float& blue) {
    		if (color.size() == 0)
    		{ return; }

    		for (unsigned int i = 0; i < color.size(); i++) {
      			ColorPoint& currC = color[i];

      			if (value < currC.val) { //找到大于当前值的第一个 ColorPoint
        			ColorPoint& prevC  = color[ max(0, (int)i - 1) ];
        			float valueDiff    = (prevC.val - currC.val);
        			float fractBetween = (valueDiff == 0) ? 0 : (value - currC.val) / valueDiff;
        			red   = (prevC.r - currC.r) * fractBetween + currC.r;
        			green = (prevC.g - currC.g) * fractBetween + currC.g;
        			blue  = (prevC.b - currC.b) * fractBetween + currC.b;
        			return;
      			}
    		}
			// 否则就是最后
    		red   = color.back().r;
    		green = color.back().g;
    		blue  = color.back().b;
    		return;
  }
    
};

要在主代码中使用它,您可以输入:

ColorGradient heatMapGradient; // 用于创建不同颜色的漂亮数组。
heatMapGradient.createDefaultHeatMapGradient();
float r, g, b;
// 依据value 在[0, 1]之间,输出相应的颜色值RGB
heatMapGradient.getColorAtValue(yourGradientValue, r,g,b); 
类型函数名参数作用
ColorGradient(){createDefaultHeatMapGradient();} //The default constructor
voidaddColorPoint(float red, float green, float blue, float value)Inserts a new color point into its correct position.
voidclearGradient()clear the array of color points
voidcreateDefaultHeatMapGradient()Places a 5 color heapmap gradient into the “color” vector.
voidgetColorAtValue(const float value, float& red, float& green, float& blue)Inputs a (value) between 0 and 1 and outputs the (red), (green) and (blue) values representing that position in the gradient.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值