Example #1
0
        private void CalcCoordsAndUpdate(ColorHandler.HSV HSV)
        {
            // Convert color to real-world coordinates and then calculate
            // the various points. HSV.Hue represents the degrees (0 to 360),
            // HSV.Saturation represents the radius.
            // This procedure doesn't draw anything--it simply
            // updates class-level variables. The UpdateDisplay
            // procedure uses these values to update the screen.

            // Given the angle (HSV.Hue), and distance from
            // the center (HSV.Saturation), and the center,
            // calculate the point corresponding to
            // the selected color, on the color wheel.
            colorPoint = GetPoint((double)HSV.Hue / 255 * 360,
                                  (double)HSV.Saturation / 255 * radius,
                                  centerPoint);

            // Given the brightness (HSV.value), calculate the
            // point corresponding to the brightness indicator.
            brightnessPoint = CalcBrightnessPoint(HSV.Value);

            // Store information about the selected color.
            brightness    = HSV.Value;
            selectedColor = ColorHandler.HSVtoColor(HSV);
            argb          = ColorHandler.HSVtoRGB(HSV);

            // The full color is the same as HSV, except that the
            // brightness is set to full (255). This is the top-most
            // color in the brightness gradient.
            fullColor = ColorHandler.HSVtoColor(HSV.Alpha, HSV.Hue, HSV.Saturation, 255);
        }
Example #2
0
 public void Draw(Graphics g, ColorHandler.ARGB argb)
 {
     // Given RGB values, calculate HSV and then update the screen.
     this.g = g;
     HSV    = ColorHandler.RGBtoHSV(argb);
     CalcCoordsAndUpdate(HSV);
     UpdateDisplay();
 }
 /// <summary>
 /// The tb alpha scroll.
 /// </summary>
 /// <param name="sender">
 /// The sender.
 /// </param>
 /// <param name="e">
 /// The e.
 /// </param>
 private void TbAlphaScroll(object sender, EventArgs e)
 {
     this.changeType = ChangeStyle.RGB;
     this.argb = new ColorHandler.ARGB(this.tbAlpha.Value, this.tbRed.Value, this.tbGreen.Value, this.tbBlue.Value);
     RefreshText(this.lblAlpha2, this.tbAlpha.Value);
     this.tbHexCode.Text = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}", this.argb.Alpha, this.argb.Red, this.argb.Green, this.argb.Blue);
     this.Invalidate();
 }
 /// <summary>
 /// The handle rgb scroll.
 /// </summary>
 /// <param name="sender">
 /// The sender.
 /// </param>
 /// <param name="e">
 /// The e.
 /// </param>
 private void HandleRGBScroll(object sender, EventArgs e)
 {
     // If the R, G, or B values change, use this
     // code to update the HSV values and invalidate
     // the color wheel (so it updates the pointers).
     // Check the isInUpdate flag to avoid recursive events
     // when you update the NumericUpdownControls.
     this.changeType = ChangeStyle.RGB;
     this.argb = new ColorHandler.ARGB(this.tbAlpha.Value, this.tbRed.Value, this.tbGreen.Value, this.tbBlue.Value);
     this.SetHSV(ColorHandler.RGBtoHSV(this.argb));
     this.SetRGBLabels(this.argb);
     this.Invalidate();
 }
 public ColorChangedEventArgs(ColorHandler.ARGB argb, ColorHandler.HSV HSV)
 {
     ARGB     = argb;
     this.HSV = HSV;
 }
Example #6
0
        public void Draw(Graphics g, Point mousePoint)
        {
            try
            {
                // You've moved the mouse.
                // Now update the screen to match.

                // Keep track of the previous color pointer point,
                // so you can put the mouse there in case the
                // user has clicked outside the circle.
                Point newColorPoint      = colorPoint;
                Point newBrightnessPoint = brightnessPoint;

                // Store this away for later use.
                this.g = g;

                if (currentState == MouseState.MouseUp)
                {
                    if (!mousePoint.IsEmpty)
                    {
                        if (colorRegion.IsVisible(mousePoint))
                        {
                            // Is the mouse point within the color circle?
                            // If so, you just clicked on the color wheel.
                            currentState = MouseState.ClickOnColor;
                        }
                        else if (brightnessRegion.IsVisible(mousePoint))
                        {
                            // Is the mouse point within the brightness area?
                            // You clicked on the brightness area.
                            currentState = MouseState.ClickOnBrightness;
                        }
                        else
                        {
                            // Clicked outside the color and the brightness
                            // regions. In that case, just put the
                            // pointers back where they were.
                            currentState = MouseState.ClickOutsideRegion;
                        }
                    }
                }

                switch (currentState)
                {
                case MouseState.ClickOnBrightness:
                case MouseState.DragInBrightness:
                    // Calculate new color information
                    // based on the brightness, which may have changed.
                    Point newPoint = mousePoint;
                    if (newPoint.Y < brightnessMin)
                    {
                        newPoint.Y = brightnessMin;
                    }
                    else if (newPoint.Y > brightnessMax)
                    {
                        newPoint.Y = brightnessMax;
                    }
                    newBrightnessPoint = new Point(brightnessX, newPoint.Y);
                    brightness         = (int)((brightnessMax - newPoint.Y) * brightnessScaling);
                    HSV.Value          = brightness;
                    brightness         = byte.MaxValue;
                    argb       = ColorHandler.HSVtoRGB(HSV);
                    brightness = (argb.Red + argb.Green + argb.Blue) / 3;
                    break;

                case MouseState.ClickOnColor:
                case MouseState.DragInColor:
                    // Calculate new color information
                    // based on selected color, which may have changed.
                    newColorPoint = mousePoint;

                    // Calculate x and y distance from the center,
                    // and then calculate the angle corresponding to the
                    // new location.
                    Point delta = new Point(
                        mousePoint.X - centerPoint.X, mousePoint.Y - centerPoint.Y);
                    int degrees = CalcDegrees(delta);

                    // Calculate distance from the center to the new point
                    // as a fraction of the radius. Use your old friend,
                    // the Pythagorean theorem, to calculate this value.
                    double distance = Math.Sqrt(delta.X * delta.X + delta.Y * delta.Y) / radius;

                    if (currentState == MouseState.DragInColor)
                    {
                        if (distance > 1)
                        {
                            // Mouse is down, and outside the circle, but you
                            // were previously dragging in the color circle.
                            // What to do?
                            // In that case, move the point to the edge of the
                            // circle at the correct angle.
                            distance      = 1;
                            newColorPoint = GetPoint(degrees, radius, centerPoint);
                        }
                    }

                    // Calculate the new HSV and RGB values.
                    HSV.Hue        = (degrees * 255 / 360);
                    HSV.Saturation = (int)(distance * 255);
                    brightness     = byte.MaxValue;
                    HSV.Value      = brightness;
                    argb           = ColorHandler.HSVtoRGB(HSV);
                    if (argb.Red < 0 || argb.Red > byte.MaxValue || argb.Green < 0 || argb.Green > byte.MaxValue || argb.Blue < 0 || argb.Blue > byte.MaxValue)
                    {
                        UpdateDisplay();
                        return;
                    }
                    brightness = (argb.Red + argb.Green + argb.Blue) / 3;
                    fullColor  = ColorHandler.HSVtoColor(HSV.Alpha, HSV.Hue, HSV.Saturation, 255);
                    break;
                }
                selectedColor = ColorHandler.HSVtoColor(HSV);

                // Raise an event back to the parent form,
                // so the form can update any UI it's using
                // to display selected color values.
                OnColorChanged(argb, HSV);

                // On the way out, set the new state.
                switch (currentState)
                {
                case MouseState.ClickOnBrightness:
                    currentState = MouseState.DragInBrightness;
                    break;

                case MouseState.ClickOnColor:
                    currentState = MouseState.DragInColor;
                    break;

                case MouseState.ClickOutsideRegion:
                    currentState = MouseState.DragOutsideRegion;
                    break;
                }

                // Store away the current points for next time.
                colorPoint      = newColorPoint;
                brightnessPoint = newBrightnessPoint;

                // Draw the gradients and points.
                UpdateDisplay();
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
        }
Example #7
0
        protected void OnColorChanged(ColorHandler.ARGB argb, ColorHandler.HSV HSV)
        {
            var e = new ColorChangedEventArgs(argb, HSV);

            ColorChanged(this, e);
        }
 public ColorChangedEventArgs(ColorHandler.ARGB argb, ColorHandler.HSV hsv)
 {
     ARGB = argb;
     HSV  = hsv;
 }
        private void CalcCoordsAndUpdate(ColorHandler.HSV HSV)
        {
            // Convert color to real-world coordinates and then calculate
            // the various points. HSV.Hue represents the degrees (0 to 360),
            // HSV.Saturation represents the radius.
            // This procedure doesn't draw anything--it simply
            // updates class-level variables. The UpdateDisplay
            // procedure uses these values to update the screen.

            // Given the angle (HSV.Hue), and distance from
            // the center (HSV.Saturation), and the center,
            // calculate the point corresponding to
            // the selected color, on the color wheel.
            colorPoint = GetPoint((double)HSV.Hue / 255 * 360,
                (double)HSV.Saturation / 255 * radius,
                centerPoint);

            // Given the brightness (HSV.value), calculate the
            // point corresponding to the brightness indicator.
            brightnessPoint = CalcBrightnessPoint(HSV.Value);

            // Store information about the selected color.
            brightness = HSV.Value;
            selectedColor = ColorHandler.HSVtoColor(HSV);
            argb = ColorHandler.HSVtoRGB(HSV);

            // The full color is the same as HSV, except that the
            // brightness is set to full (255). This is the top-most
            // color in the brightness gradient.
            fullColor = ColorHandler.HSVtoColor(HSV.Alpha, HSV.Hue, HSV.Saturation, 255);
        }
        public void Draw(Graphics g, Point mousePoint)
        {
            try
            {
                // You've moved the mouse.
                // Now update the screen to match.

                // Keep track of the previous color pointer point,
                // so you can put the mouse there in case the
                // user has clicked outside the circle.
                Point newColorPoint = colorPoint;
                Point newBrightnessPoint = brightnessPoint;

                // Store this away for later use.
                this.g = g;

                if (currentState == MouseState.MouseUp)
                {
                    if (!mousePoint.IsEmpty)
                    {
                        if (colorRegion.IsVisible(mousePoint))
                        {
                            // Is the mouse point within the color circle?
                            // If so, you just clicked on the color wheel.
                            currentState = MouseState.ClickOnColor;
                        }
                        else if (brightnessRegion.IsVisible(mousePoint))
                        {
                            // Is the mouse point within the brightness area?
                            // You clicked on the brightness area.
                            currentState = MouseState.ClickOnBrightness;
                        }
                        else
                        {
                            // Clicked outside the color and the brightness
                            // regions. In that case, just put the
                            // pointers back where they were.
                            currentState = MouseState.ClickOutsideRegion;
                        }
                    }
                }

                switch (currentState)
                {
                    case MouseState.ClickOnBrightness:
                    case MouseState.DragInBrightness:
                        // Calculate new color information
                        // based on the brightness, which may have changed.
                        Point newPoint = mousePoint;
                        if (newPoint.Y < brightnessMin)
                        {
                            newPoint.Y = brightnessMin;
                        }
                        else if (newPoint.Y > brightnessMax)
                        {
                            newPoint.Y = brightnessMax;
                        }

                        newBrightnessPoint = new Point(brightnessX, newPoint.Y);
                        brightness = (int)((brightnessMax - newPoint.Y) * brightnessScaling);
                        hsv.Value = brightness;
                        brightness = byte.MaxValue;
                        argb = ColorHandler.HSVtoRGB(hsv);
                        brightness = (argb.Red + argb.Green + argb.Blue) / 3;
                        break;

                    case MouseState.ClickOnColor:
                    case MouseState.DragInColor:
                        // Calculate new color information
                        // based on selected color, which may have changed.
                        newColorPoint = mousePoint;

                        // Calculate x and y distance from the center,
                        // and then calculate the angle corresponding to the
                        // new location.
                        Point delta = new Point(
                            mousePoint.X - centerPoint.X, mousePoint.Y - centerPoint.Y);
                        int degrees = CalcDegrees(delta);

                        // Calculate distance from the center to the new point
                        // as a fraction of the radius. Use your old friend,
                        // the Pythagorean theorem, to calculate this value.
                        double distance = Math.Sqrt(delta.X * delta.X + delta.Y * delta.Y) / radius;

                        if (currentState == MouseState.DragInColor)
                        {
                            if (distance > 1)
                            {
                                // Mouse is down, and outside the circle, but you
                                // were previously dragging in the color circle.
                                // What to do?
                                // In that case, move the point to the edge of the
                                // circle at the correct angle.
                                distance = 1;
                                newColorPoint = GetPoint(degrees, radius, centerPoint);
                            }
                        }

                        // Calculate the new HSV and RGB values.
                        hsv.Hue = (degrees * 255 / 360);
                        hsv.Saturation = (int)(distance * 255);
                        brightness = byte.MaxValue;
                        hsv.Value = brightness;
                        argb = ColorHandler.HSVtoRGB(hsv);
                        if (argb.Red < 0 || argb.Red > byte.MaxValue || argb.Green < 0 || argb.Green > byte.MaxValue || argb.Blue < 0 || argb.Blue > byte.MaxValue)
                        {
                            UpdateDisplay();
                            return;
                        }

                        brightness = (argb.Red + argb.Green + argb.Blue) / 3;
                        fullColor = ColorHandler.HSVtoColor(hsv.Alpha, hsv.Hue, hsv.Saturation, 255);
                        break;
                }

                selectedColor = ColorHandler.HSVtoColor(hsv);

                // Raise an event back to the parent form,
                // so the form can update any UI it's using
                // to display selected color values.
                OnColorChanged(argb, hsv);

                // On the way out, set the new state.
                switch (currentState)
                {
                    case MouseState.ClickOnBrightness:
                        currentState = MouseState.DragInBrightness;
                        break;
                    case MouseState.ClickOnColor:
                        currentState = MouseState.DragInColor;
                        break;
                    case MouseState.ClickOutsideRegion:
                        currentState = MouseState.DragOutsideRegion;
                        break;
                }

                // Store away the current points for next time.
                colorPoint = newColorPoint;
                brightnessPoint = newBrightnessPoint;

                // Draw the gradients and points.
                UpdateDisplay();
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
        }