当前位置:   article > 正文

颜色模型RGB,HSV颜色模型的数值之间相互转换_rgb转hsv各个值验证

rgb转hsv各个值验证

 公式详解 参考文献 : http://blog.csdn.net/idfaya/article/details/6770414

向作者白哦大十二分的敬意。

因为是做游戏的,所以才会有这方面的需求,我们的引擎采用的脚本是 lua, 我是通过lua将数据转换后为游戏材质属性赋值,因为下面的脚本运行速率并不是太高,所以就先在脚本中实现,方便调试。


--[[
 * RGB颜色模型转化为HSV的颜色模型数据
 * h,s,v 色相,饱和度, 色调(明度)
 * 返回值 hsvValue h(0-360),s(0-255) v(0-255)
 * @param colorRGB 颜色数值{r=0,g=255,b=100,a=255}, 数值部分0-255
]]
function _G.RGBConvertToHSV(colorRGB)
	local r,g,b = colorRGB.r, colorRGB.g, colorRGB.b;
	local h,s,v = 0,0,0;

    local max1 = math.max(r, math.max(g,b));
    local min1 = math.min(r, math.min(g,b));

    if max1 == min1 then
    	h=0;
    else
    	if r == max1 then
	    	if g >= b then
	    		h = 60 * (g-b) / (max1-min1);
	    	else
	    		h = 60 * (g-b) / (max1-min1) + 360;
	    	end    	
	    end
	    if g == max1 then
	    	h = 60 * (b-r)/(max1-min1) + 120;
	    end
	    if b == max1 then
	    	h = 60 * (r-g)/(max1-min1) + 240;
	    end
    end    
   	
   	if max1 == 0 then
   		s = 0;
   	else
   		s = (1- min1 / max1) * 255;
   	end
    
    v = max1;
    
    local hsvValue = {
    	h = h;
    	s = s;
    	v= v;
    };

    return hsvValue;
end

--[[
 * HSV to RGB 
 * h,s,v 色相,饱和度,明度(色调)
 * 返回值 colorValue 颜色值 (0-1)规范化数值
 * @paream colorHSV 颜色数值{h,s,v}, h(0-360) s(0-255) v(0-255)
]]
function HSVConvertToRGB( colorHSV )
	local cr,cg,cb = 0,0,0;
	
	colorHSV.s = colorHSV.s /255;
 	colorHSV.v = colorHSV.v / 255;
		
	if( colorHSV.s == 0 ) then
	 	cr,cg,cb = colorHSV.v, colorHSV.v, colorHSV.v;
		
	else
	 	colorHSV.h = colorHSV.h /60;

	 	local i = math.floor(colorHSV.h);
	 	local f = colorHSV.h - i;
	 	local a = colorHSV.v * ( 1 - colorHSV.s );
	 	local b = colorHSV.v * ( 1 - colorHSV.s * f );
        local c = colorHSV.v * ( 1 - colorHSV.s * (1 - f ) );        
                
        if i== 0 then
        	cr,cg,cb = colorHSV.v, c, a;
    	elseif i == 1 then
    		cr,cg,cb = b, colorHSV.v, a;
		elseif i == 2 then
			cr,cg,cb = a, colorHSV.v, c;
		elseif i == 3 then
			cr,cg,cb = a, b, colorHSV.v;
		elseif i == 4 then
			cr,cg,cb = c, a, colorHSV.v;
		else
			cr,cg,cb = colorHSV.v, a, b;
		end
    end   

    local rgbValue = {r = cr, g = cg, b= cb, a=1};
    return rgbValue;
end 


--[[
 *HSL颜色值转换为RGB. 
 * h, s, l 色相,饱和度,亮度
 * h(0-360) s(0-255) l(0-255);
 * 返回的 color 数值 [0,1]
 * @param hslValue
]]
function HSLConvertToRGB(hslValue)
    local h,s,l = hslValue.h, hslValue.s, hslValue.l;
    h = h/360;
    s = s/255;
    l = l/255;

    local r, g, b;

    if s == 0 then
        r, g, b = l, l, l;
    else
        local hue2rgb = function(p, q, t)
            if t < 0 then t = t +1; end
            if t > 1 then t = t -1; end

            if t < 1/6 then return p + (q - p) * 6 * t; end
            if t < 1/2 then return q; end
            if t < 2/3 then return p + (q - p) * (2/3 - t) * 6; end

            return p;
        end

        local q = l < 0.5 and l * (1 + s) or l + s - l * s;
        local p = 2 * l - q;
        r = hue2rgb(p, q, h + 1/3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1/3);
    end

    local rgbValue = {r = r, g = g, b= b, a=1};
    return rgbValue;
end


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/94610
推荐阅读
相关标签
  

闽ICP备14008679号