欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HSV与RGB色彩模型互相转换

程序员文章站 2022-06-11 16:14:37
...

    HSV与RGB是ArcGIS中用的最多的两种色彩模型,这两种色彩模型基本信息就不做过多介绍了。下面是互相转换的代码

1、HSV转RGB 

     Arcgis Engine中可以将 Symbol中的Color先转为 IHsvColor这个接口,在这个接口中获取H、S、V三个值,我这里方便计算,将S、V值除以了100。

            IHsvColor pHsvColor = pSetFillSymbol.Color as IHsvColor;
            if(pHsvColor!=null&&pHsvColor.NullColor==false)
            {
                int H = pHsvColor.Hue;
                double S = Convert.ToDouble(pHsvColor.Saturation)/100;
                double V = Convert.ToDouble(pHsvColor.Value) / 100;
                Color pfill_Color = HSVToRGB(H,S,V);
                this.cPEFill.Color = pfill_Color;
            }
        /// 将HSV色彩模型转换为RGB模型
        /// </summary>
        /// <param name="H">色调 0-360</param>
        /// <param name="S">饱和度 0-1</param>
        /// <param name="V">亮度 0-1</param>
        /// <returns></returns>
        private Color HSVToRGB(int H,double S,double V)
        {
            if ((H >= 0 && H <= 360) && (S >= 0 && S <= 1) && (V >= 0 && V <= 1))
            {
                int R=0, G=0, B = 0;
                double _R=0, _G=0, _B = 0;
                var C = V * S;                
                var X = C * (1 - Math.Abs((H / 60)%2-1));
                var m = V - C;
                if (H >= 0 && H < 60)
                {
                    _R = C;
                    _G = X;
                    _B = 0;
                }
                else if(H >= 60 && H < 120)
                {
                    _R = X;
                    _G = C;
                    _B = 0;
                }
                else if(H > 120 && H <180)
                {
                    _R = 0;
                    _G = C;
                    _B = X;
                }
                else if(H >= 180 && H < 240)
                {
                    _R = 0;
                    _G = X;
                    _B = C;
                }
                else if(H >= 240 && H < 300)
                {
                    _R = X;
                    _G = 0;
                    _B = C;
                }
                else if (H >= 300 && H < 360)
                {
                    _R = C;
                    _G = 0;
                    _B = X;
                }
                 R = Convert.ToInt32((_R + m) * 255);
                 G = Convert.ToInt32((_G + m) * 255);
                 B = Convert.ToInt32((_B + m) * 255);

                return Color.FromArgb(R, G, B);
            }
            else
            {
                return Color.FromArgb(0, 0, 0);
            }
        }

2、RGB转HSV

/// <summary>
        /// 将RGB色彩模型转换为HSV色彩模型
        /// </summary>
        /// <param name="R">0-255</param>
        /// <param name="G">0-255</param>
        /// <param name="B">0-255</param>
        /// <returns></returns>
        private IHsvColor RGBToHSV(int R,int G,int B)
        {
            double _R = 0, _G = 0, _B = 0;
            _R = Convert.ToDouble(R) / 255;
            _G = Convert.ToDouble(G) / 255;
            _B = Convert.ToDouble(B) / 255;
            double[] _RGB = new double[] {_R,_G,_B };
            var C_max = _RGB.Max();
            var C_min = _RGB.Min();
            var D_value = C_max - C_min;
            double H = 0, S = 0, V = 0;
            if(D_value==0)
            {
                H = 0;
            }
            else if(C_max==_R)
            {
                H = (((_G - _B) / D_value) % 6) * 60;
            }
            else if(C_max==_G)
            {
                H = (((_B - _R) / D_value) + 2) * 60;
            }
            else if(C_max==_B)
            {
                H = (((_R - _G) / D_value) + 4) * 60;
            }

            if (C_max == 0)
            {
                S = 0;
            }
            else
            {
                S = D_value / C_max;
            }
            V = C_max;

            IHsvColor pHsvColor = new HsvColorClass();
            pHsvColor.Hue = Convert.ToInt32(H);
            pHsvColor.Saturation = Convert.ToInt32(S*100);
            pHsvColor.Value = Convert.ToInt32(V*100);
            return pHsvColor;
        }


相关标签: GIS