Esempio n. 1
0
        public static Color HSL2RGB( HSL hsl )
        {
            // by Donald (Sterex 1996), [email protected], 20011124
            int lMax = (int)(hsl.L * 255.0);
            if ( hsl.S > 0 ) {
                int lMin = (int)((100 - hsl.S * 100.0) * (double)lMax / 100.0);
                double q = (lMax - lMin) / 60;

                int r, g, b, lMid;
                int h = (int)(hsl.H * 360);
                if ( h < 60 ) {
                    lMid = (int)((h - 0) * q + lMin);
                    r = lMax; g = lMid; b = lMin;
                }
                else if ( h < 120 ) {
                    lMid = (int)((-(h - 120)) * q + lMin);
                    r = lMid; g = lMax; b = lMin;
                }
                else if ( h < 180 ) {
                    lMid = (int)((h - 120) * q + lMin);
                    r = lMin; g = lMax; b = lMid;
                }
                else if ( h < 240 ) {
                    lMid = (int)((-(h - 240)) * q + lMin);
                    r = lMin; g = lMid; b = lMax;
                }
                else if ( h < 300 ) {
                    lMid = (int)((h - 240) * q + lMin);
                    r = lMid; g = lMin; b = lMax;
                }
                else {
                    lMid = (int)((-(h - 360)) * q + lMin);
                    r = lMax; g = lMin; b = lMid;
                }

                return Color.FromArgb( r, g, b );
            }
            else {
                return Color.FromArgb( lMax, lMax, lMax );
            }
        }
Esempio n. 2
0
        public static HSL RGB2HSLX( Color c )
        {
            if ( init == 0 ) {
                for ( init = 2; init < 256; ++init ) {
                    Lum[init] = ((double)init)/255;
                }
                for ( init = 1; init < 256; ++init ) {
                    QTab[init] = 60/init;
                }
            }

            int lMax, lMin;
            if ( c.R > c.G ) {
                lMax = c.R; lMin = c.G;
            }
            else {
                lMax = c.G; lMin = c.R;
            }
            if ( c.B > lMax )
                lMax = c.B;
            else if ( c.B < lMin ) {
                lMin = c.B;
            }

            HSL hsl = new HSL();
            hsl.L = Lum[lMax];

            int lDifference = lMax - lMin;
            if ( lDifference > 0 ) {
                // do a 65K 2D lookup table here for more speed if needed
                hsl.S = ((double)lDifference) / lMax;
                if ( lMax == c.R ) {
                    if ( c.B > c.G )
                        hsl.H = ((QTab[lDifference] * (c.G - c.B)) / 360) + 1;
                    else
                        hsl.H = ((QTab[lDifference] * (c.G - c.B)) / 360);
                }
                else if ( lMax == c.G ) {
                    hsl.H = (QTab[lDifference] * (c.B - c.R) + 120) / 360;
                }
                else {
                    hsl.H = (QTab[lDifference] * (c.R - c.G) + 240) / 360;
                }
            }
            else {
                hsl.S = 0;
                hsl.H = 0;
            }

            return hsl;
        }
Esempio n. 3
0
        /// <summary>
        /// Converts a colour from HSL to RGB
        /// </summary>
        /// <remarks>Adapted from the algoritm in Foley and Van-Dam</remarks>
        /// <param name="hsl">The HSL value</param>
        /// <returns>A Color structure containing the equivalent RGB values</returns>
        public static Color HSL2RGBx( HSL hsl )
        {
            double  r=0,g=0,b=0;
            double temp1,temp2;

            if(hsl.L==0) {
                r=g=b=0;
            }
            else {
                if(hsl.S==0) {
                    r=g=b=hsl.L;
                }
                else {

                    temp2 = ((hsl.L<=0.5) ? hsl.L*(1.0+hsl.S) : hsl.L+hsl.S-(hsl.L*hsl.S));
                    temp1 = 2.0*hsl.L-temp2;

                    double[] t3=new double[]{hsl.H+1.0/3.0,hsl.H,hsl.H-1.0/3.0};
                    double[] clr=new double[]{0,0,0};

                    for(int i=0;i<3;i++) {
                        if(t3[i]<0)
                            t3[i]+=1.0;

                        if(t3[i]>1)
                            t3[i]-=1.0;

                        if(6.0*t3[i] < 1.0)
                            clr[i]=temp1+(temp2-temp1)*t3[i]*6.0;
                        else if(2.0*t3[i] < 1.0)
                            clr[i]=temp2;
                        else if(3.0*t3[i] < 2.0)
                            clr[i]=(temp1+(temp2-temp1)*((2.0/3.0)-t3[i])*6.0);
                        else
                            clr[i]=temp1;
                    }

                    r=clr[0];
                    g=clr[1];
                    b=clr[2];
                }
            }

            return Color.FromArgb((int)(255*r),(int)(255*g),(int)(255*b));
        }