Example #1
0
File: HSB.cs Project: angel2230/ZZZ
        public override bool Equals(object obj)
        {
            bool equal = false;

            if (obj is HSB)
            {
                HSB hsb = ( HSB )obj;

                if (this.Hue == hsb.Hue && this.Saturation == hsb.Saturation && this.Brightness == hsb.Brightness)
                {
                    equal = true;
                }
            }

            return(equal);
        }
Example #2
0
        /// <summary>
        /// Updates the color slider.
        /// </summary>

        private void UpdateColorSlider()
        {
            RGB       rgb    = ( RGB )this.rgbColorSpace.Structure;
            HSB       hsb    = ( HSB )this.hsbColorSpace.Structure;
            Rectangle region = new Rectangle(0, -1, 18, 257);

            using (Graphics g = Graphics.FromImage(m_colorSliderBitmap)) {
                if (this.m_currentColorSpace is RgbColorSpace)
                {
                    char dChar = this.m_currentColorSpace.SelectedComponent.DisplayCharacter;
                    int  red   = rgb.Red;
                    int  green = rgb.Green;
                    int  blue  = rgb.Blue;

                    Color startColor;
                    Color endColor;

                    switch (dChar)
                    {
                    case 'R':
                        startColor = Color.FromArgb(0, green, blue);
                        endColor   = Color.FromArgb(255, green, blue);
                        break;

                    case 'G':
                        startColor = Color.FromArgb(red, 0, blue);
                        endColor   = Color.FromArgb(red, 255, blue);
                        break;

                    default:
                        startColor = Color.FromArgb(red, green, 0);
                        endColor   = Color.FromArgb(red, green, 255);
                        break;
                    }

                    using (LinearGradientBrush lgb = new LinearGradientBrush(region, startColor, endColor, 270f)) {
                        g.FillRectangle(lgb, region);
                    }
                }
                else if (this.m_currentColorSpace is HsbColorSpace)
                {
                    if (this.m_currentColorSpace.SelectedComponent.DisplayCharacter == 'H')
                    {
                        Rectangle rect = new Rectangle(0, 0, 20, 256);

                        using (LinearGradientBrush brBrush = new LinearGradientBrush(rect, Color.Blue, Color.Red, 90f, false)) {
                            Color[] colorArray = { Color.Red, Color.Magenta, Color.Blue, Color.Cyan, Color.FromArgb(0, 255, 0), Color.Yellow, Color.Red };
                            float[] posArray   = { 0.0f, 0.1667f, 0.3372f, 0.502f, 0.6686f, 0.8313f, 1.0f };

                            ColorBlend colorBlend = new ColorBlend();
                            colorBlend.Colors           = colorArray;
                            colorBlend.Positions        = posArray;
                            brBrush.InterpolationColors = colorBlend;

                            g.FillRectangle(brBrush, region);
                        }
                    }
                    else if (this.m_currentColorSpace.SelectedComponent.DisplayCharacter == 'B')
                    {
                        RGB sRgb = ColorConverter.HsbToRgb(new HSB(hsb.Hue, hsb.Saturation, 100));
                        RGB eRgb = ColorConverter.HsbToRgb(new HSB(hsb.Hue, hsb.Saturation, 0));

                        using (LinearGradientBrush lgb = new LinearGradientBrush(region, Color.FromArgb(sRgb.Red, sRgb.Green, sRgb.Blue), Color.FromArgb(eRgb.Red, eRgb.Green, eRgb.Blue), 90f)) {
                            g.FillRectangle(lgb, region);
                        }
                    }
                    else
                    {
                        RGB sRgb = ColorConverter.HsbToRgb(new HSB(hsb.Hue, 100, hsb.Brightness));
                        RGB eRgb = ColorConverter.HsbToRgb(new HSB(hsb.Hue, 0, hsb.Brightness));

                        using (LinearGradientBrush lgb = new LinearGradientBrush(region, Color.FromArgb(sRgb.Red, sRgb.Green, sRgb.Blue), Color.FromArgb(eRgb.Red, eRgb.Green, eRgb.Blue), 90f)) {
                            g.FillRectangle(lgb, region);
                        }
                    }
                }
            }

            this.picColorSlider.Image = m_colorSliderBitmap;
        }
Example #3
0
        private Color CalculateSelectedColor(Point p)
        {
            IColorSpaceStructure selectedColor = null;

            if (m_colorSpace is HsbColorSpace)
            {
                HSB hsb = ( HSB )m_colorSpace.Structure;

                if (m_component == 'H')
                {
                    int brightness = ( int )((( double )255 - p.Y) / 2.55);
                    int saturation = ( int )(( double )p.X / 2.55);

                    selectedColor = new HSB(hsb.Hue, saturation, brightness);
                }
                else if (m_component == 'S')
                {
                    int hue        = ( int )(p.X * (( double )360 / 255));
                    int brightness = ( int )((( double )255 - p.Y) / 2.55);

                    if (hue == 360)
                    {
                        hue = 0;
                    }

                    selectedColor = new HSB(hue, hsb.Saturation, brightness);
                }
                else if (m_component == 'B')
                {
                    int hue        = ( int )(p.X * (( double )360 / 255));
                    int saturation = ( int )((( double )255 - p.Y) / 2.55);

                    if (hue == 360)
                    {
                        hue = 0;
                    }

                    selectedColor = new HSB(hue, saturation, hsb.Brightness);
                }
            }
            else if (m_colorSpace is RgbColorSpace)
            {
                RGB rgb = ( RGB )m_colorSpace.Structure;

                if (m_component == 'R')
                {
                    selectedColor = new RGB(rgb.Red, 255 - p.Y, p.X);
                }
                else if (m_component == 'G')
                {
                    selectedColor = new RGB(255 - p.Y, rgb.Green, p.X);
                }
                else if (m_component == 'B')
                {
                    selectedColor = new RGB(p.X, 255 - p.Y, rgb.Blue);
                }
            }

            RGB crgb;
            HSB hsbSelectedColor;

            if ((hsbSelectedColor = selectedColor as HSB) != null)
            {
                crgb = ColorConverter.HsbToRgb(hsbSelectedColor);
            }
            else
            {
                crgb = ( RGB )selectedColor;
            }

            return(ColorConverter.RgbToColor(crgb));
        }
Example #4
0
        private void UpdateCurrentColorBitmap(bool resetPreviouslyPickedPoint)
        {
            if (!m_isMouseDown)
            {
                int x = 0;
                int y = 0;

                if (m_currentColorBitmap != null)
                {
                    m_currentColorBitmap.Dispose();
                }

                if (m_colorSpace is HsbColorSpace)
                {
                    HSB hsb = ( HSB )m_colorSpace.Structure;

                    if (m_component == 'H')
                    {
                        Color c = m_color;
                        if (c.Equals(Color.FromArgb(0, 0, 0, 0)) | c.Equals(Color.FromArgb(0, 0, 0)))
                        {
                            c = Color.FromArgb(255, 0, 0);
                        }

                        m_currentColorBitmap = ColorRenderingHelper.GetHueColorField(c);

                        x = ( int )Math.Round(hsb.Saturation * 2.55);
                        y = 255 - ( int )Math.Round(hsb.Brightness * 2.55);
                    }
                    else if (m_component == 'S')
                    {
                        m_currentColorBitmap = ColorRenderingHelper.GetSaturationColorField(m_selectedComponentValue);

                        x = ( int )Math.Ceiling(hsb.Hue * (( double )255 / 360));
                        y = ( int )(255 - (hsb.Brightness * 2.55));
                    }
                    else if (m_component == 'B')
                    {
                        m_currentColorBitmap = ColorRenderingHelper.GetBrightnessColorField(m_selectedComponentValue);

                        x = ( int )Math.Ceiling(hsb.Hue * (( double )255 / 360));
                        y = 255 - ( int )Math.Round(hsb.Saturation * 2.55);
                    }
                }
                else if (m_colorSpace is RgbColorSpace)
                {
                    Color c = (( RgbColorSpace )m_colorSpace).GetColor();

                    if (m_component == 'R')
                    {
                        m_currentColorBitmap = ColorRenderingHelper.GetRedColorField(m_selectedComponentValue);

                        x = c.B;
                        y = 255 - c.G;
                    }
                    else if (m_component == 'G')
                    {
                        m_currentColorBitmap = ColorRenderingHelper.GetGreenColorField(m_selectedComponentValue);

                        x = c.B;
                        y = 255 - c.R;
                    }
                    else if (m_component == 'B')
                    {
                        m_currentColorBitmap = ColorRenderingHelper.GetBlueColorField(m_selectedComponentValue);

                        x = c.R;
                        y = 255 - c.G;
                    }
                }

                if (resetPreviouslyPickedPoint)
                {
                    m_currentPoint = new Point(x, y);
                }
            }
        }
Example #5
0
        }         // RgbToHsb

        /// <summary>
        /// Converts HSB to RGB.
        /// </summary>
        /// <param name="rgb">A HSB object containing the HSB values that are to
        /// be converted to RGB values.</param>
        /// <returns>A RGB equivalent.</returns>

        internal static RGB HsbToRgb(HSB hsb)
        {
            double h, s, b;
            double red = 0, green = 0, blue = 0;

            h = hsb.Hue;
            s = (( double )hsb.Saturation) / 100;
            b = (( double )hsb.Brightness) / 100;

            if (s == 0)
            {
                red   = b;
                green = b;
                blue  = b;
            }
            else
            {
                double p, q, t;

                // the color wheel has six sectors.
                double fractionalSector;
                int    sectorNumber;
                double sectorPosition;

                sectorPosition   = h / 60;
                sectorNumber     = ( int )Math.Floor(sectorPosition);
                fractionalSector = sectorPosition - sectorNumber;

                p = b * (1 - s);
                q = b * (1 - (s * fractionalSector));
                t = b * (1 - (s * (1 - fractionalSector)));

                // Assign the fractional colors to r, g, and b
                // based on the sector the angle is in.
                switch (sectorNumber)
                {
                case 0:
                    red   = b;
                    green = t;
                    blue  = p;
                    break;

                case 1:
                    red   = q;
                    green = b;
                    blue  = p;
                    break;

                case 2:
                    red   = p;
                    green = b;
                    blue  = t;
                    break;

                case 3:
                    red   = p;
                    green = q;
                    blue  = b;
                    break;

                case 4:
                    red   = t;
                    green = p;
                    blue  = b;
                    break;

                case 5:
                    red   = b;
                    green = p;
                    blue  = q;
                    break;
                }
            }

            int nRed, nGreen, nBlue;

            nRed   = ( int )Math.Round(red * 255);
            nGreen = ( int )Math.Round(green * 255);
            nBlue  = ( int )Math.Round(blue * 255);

            return(new RGB(nRed, nGreen, nBlue));
        }         // HsbToRgb
Example #6
0
        private Color CalculateSelectedColor( Point p )
        {
            IColorSpaceStructure selectedColor = null;

            if ( m_colorSpace is HsbColorSpace ) {

                HSB hsb = ( HSB ) m_colorSpace.Structure;

                if ( m_component == 'H' ) {

                    int brightness = ( int ) ( ( ( double ) 255 - p.Y ) / 2.55 );
                    int saturation = ( int ) ( ( double ) p.X / 2.55 );

                    selectedColor = new HSB( hsb.Hue, saturation, brightness );

                } else if ( m_component == 'S' ) {

                    int hue = ( int ) ( p.X * ( ( double ) 360 / 255 ) );
                    int brightness = ( int ) ( ( ( double ) 255 - p.Y ) / 2.55 );

                    if ( hue == 360 ) {
                        hue = 0;
                    }

                    selectedColor = new HSB( hue, hsb.Saturation, brightness );

                } else if ( m_component == 'B' ) {

                    int hue = ( int ) ( p.X * ( ( double ) 360 / 255 ) );
                    int saturation = ( int ) ( ( ( double ) 255 - p.Y ) / 2.55 );

                    if ( hue == 360 ) {
                        hue = 0;
                    }

                    selectedColor = new HSB( hue, saturation, hsb.Brightness );

                }

            } else if ( m_colorSpace is RgbColorSpace ) {

                RGB rgb = ( RGB ) m_colorSpace.Structure;

                if ( m_component == 'R' ) {
                    selectedColor = new RGB( rgb.Red, 255 - p.Y, p.X );
                } else if ( m_component == 'G' ) {
                    selectedColor = new RGB( 255 - p.Y, rgb.Green, p.X );
                } else if ( m_component == 'B' ) {
                    selectedColor = new RGB( p.X, 255 - p.Y, rgb.Blue);
                }

            }

            RGB crgb;
            HSB hsbSelectedColor;
            if ( ( hsbSelectedColor = selectedColor as HSB ) != null ) {
                crgb = ColorConverter.HsbToRgb( hsbSelectedColor );
            } else {
                crgb = ( RGB ) selectedColor;
            }

            return ColorConverter.RgbToColor( crgb );
        }
Example #7
0
        /// <summary>
        /// Converts HSB to RGB.
        /// </summary>
        /// <param name="rgb">A HSB object containing the HSB values that are to 
        /// be converted to RGB values.</param>
        /// <returns>A RGB equivalent.</returns>
        internal static RGB HsbToRgb( HSB hsb )
        {
            double h, s, b;
            double red = 0, green = 0, blue = 0;

            h = hsb.Hue;
            s = ( ( double ) hsb.Saturation ) / 100;
            b = ( ( double ) hsb.Brightness ) / 100;

            if ( s == 0 ) {

                red = b;
                green = b;
                blue = b;

            } else {

                double p, q, t;

                // the color wheel has six sectors.
                double fractionalSector;
                int sectorNumber;
                double sectorPosition;

                sectorPosition = h / 60;
                sectorNumber = ( int ) Math.Floor( sectorPosition );
                fractionalSector = sectorPosition - sectorNumber;

                p = b * ( 1 - s );
                q = b * ( 1 - ( s * fractionalSector ) );
                t = b * ( 1 - ( s * ( 1 - fractionalSector ) ) );

                // Assign the fractional colors to r, g, and b
                // based on the sector the angle is in.
                switch (sectorNumber) {
                    case 0:
                        red = b;
                        green = t;
                        blue = p;
                        break;

                    case 1:
                        red = q;
                        green = b;
                        blue = p;
                        break;

                    case 2:
                        red = p;
                        green = b;
                        blue = t;
                        break;

                    case 3:
                        red = p;
                        green = q;
                        blue = b;
                        break;

                    case 4:
                        red = t;
                        green = p;
                        blue = b;
                        break;

                    case 5:
                        red = b;
                        green = p;
                        blue = q;
                        break;
                }

            }

            int nRed, nGreen, nBlue;
            nRed = ( int ) Math.Round( red * 255 );
            nGreen = ( int ) Math.Round( green * 255 );
            nBlue = ( int ) Math.Round( blue * 255 );

            return new RGB( nRed, nGreen, nBlue );
        }