Beispiel #1
0
        private void CalcCoordsAndUpdate(DRColor.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_norm.
            // 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 = DRColor.HSVtoColor(HSV);
            RGB           = new DRColor.RGB(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 = DRColor.HSVtoColor(new DRColor.HSV(HSV.Hue, HSV.Saturation, 255));
        }
Beispiel #2
0
 public static DRColor.RGB ProperMix(DRColor.RGB a, DRColor.RGB b, double ratio)
 {
     DRColor.RGB result = new DRColor.RGB();
     result.Red = properMixAlgo(b.Red, a.Red);
     result.Green = properMixAlgo(b.Green, a.Green);
     result.Blue = properMixAlgo(b.Blue, a.Blue);
     return result;
 }
Beispiel #3
0
 public void addColor(DRColor.RGB rgb)
 {
     if (mood.Color_List.Count == 10)
     {
         mood.Color_List.RemoveAt(0);
     }
     mood.Color_List.Add(rgb);
 }
Beispiel #4
0
        public static void fillBoth(DRColor.RGB rgb)
        {
            if (rgb == null) return;

            for (int i = 0; i < Globals.KaiLength; i++)
            {
                Rainbow.KaiSet(i, rgb);
            }
            update();
        }
Beispiel #5
0
        public static DRColor.RGB MagicMix(DRColor.RGB a, DRColor.RGB b, double ratio)
        {
            DRColor.RGB result = new DRColor.RGB();
            int redDistance = (b.Red - a.Red);
            int greenDistance = (b.Green - a.Green);
            int blueDistance = (b.Blue - a.Blue);

            result.Red = (int)(a.Red + redDistance * ratio);
            result.Green = (int)(a.Green + greenDistance * ratio);
            result.Blue = (int)(a.Blue + blueDistance * ratio);

            return result;
        }
Beispiel #6
0
        private Color[] GetColors()
        {
            // Create an array of COLOR_COUNT
            // colors, looping through all the
            // hues between 0 and 255, broken
            // into COLOR_COUNT intervals. HSV is
            // particularly well-suited for this,
            // because the only value that changes
            // as you create colors is the Hue.
            Color[] Colors = new Color[COLOR_COUNT];

            for (int i = 0; i <= COLOR_COUNT - 1; i++)
            {
                Color temp = DRColor.HSVtoColor(new DRColor.HSV((int)((double)(i * 255) / COLOR_COUNT), 255, 255));
                int   r    = max(temp.R * 2);
                int   g    = max(temp.G * 2);
                int   b    = max(temp.B * 2);
                Colors[i] = Color.FromArgb(r, g, b);
            }
            return(Colors);
        }
Beispiel #7
0
        protected override void OnPaint(PaintEventArgs pe)
        {
            // Call the OnPaint method of the base class.
            base.OnPaint(pe);

            if (mood.Color_List.Count != 0)
            {
                Graphics g = pe.Graphics;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                Size rect_size = new Size(this.ClientSize.Width / mood.Color_List.Count, this.ClientSize.Height);
                int  count     = 0;
                foreach (DRColor.RGB rgb in mood.Color_List)
                {
                    Point      p       = new Point(count * rect_size.Width, 0);
                    SolidBrush myBrush = new SolidBrush(DRColor.RGBtoColor(RainbowUtils.increaseBrightness(rgb, 130)));
                    g.FillEllipse(myBrush, new Rectangle(p, rect_size));
                    myBrush.Dispose();
                    count++;
                }
            }
        }
Beispiel #8
0
        // Immediately updates the ardunio's buffer for the color
        private static bool sendClr(int pos, DRColor.RGB rgb, int strip)
        {
            posClamp(ref pos, 1);

            // Create color packet, add 64 for second led strip
            byte[] bytes = new byte[4];
            bytes[0] = (byte)pos;
            if (strip == 2) bytes[0] += (byte)64;
            bytes[1] = (byte)rgb.Red;
            bytes[2] = (byte)rgb.Green;
            bytes[3] = (byte)rgb.Blue;

            int bytesWritten = 0;
            ErrorCode ec = ErrorCode.None;

            if (Connected)
            {
                ec = writer.Write(bytes, 2000, out bytesWritten);
            }
            if(ec == ErrorCode.ResourceBusy)
            {
                return true;
            }

            if (ec != ErrorCode.None)
            {
                throw new Exception(UsbDevice.LastErrorString);
            }

            return true;
        }
Beispiel #9
0
 public static void ZenSet(int pos, DRColor.RGB rgb)
 {
     posClamp(ref pos, 2);
     _zen[pos] = rgb;
 }
Beispiel #10
0
 private static DRColor.RGB attenuateStrip(DRColor.RGB clr)
 {
     float dim = 0.3f;
     DRColor.RGB new_clr = new DRColor.RGB((int)(clr.Red * dim), (int)(clr.Green * dim), (int)(clr.Blue * dim));
     return new_clr;
 }
Beispiel #11
0
        public ColorWheel(Rectangle colorRectangle, Rectangle brightnessRectangle, Rectangle selectedColorRectangle, DRColor.HSV default_color)
        {
            // Caller must provide locations for color wheel
            // (colorRectangle), brightness "strip" (brightnessRectangle)
            // and location to display selected color (selectedColorRectangle).

            using (GraphicsPath path = new GraphicsPath())
            {
                // Store away locations for later use.
                this.colorRectangle = colorRectangle;
                this.brightnessRectangle = brightnessRectangle;
                this.selectedColorRectangle = selectedColorRectangle;

                // Calculate the center of the circle.
                // Start with the location, then offset
                // the point by the radius_norm.
                // Use the smaller of the width and height of
                // the colorRectangle value.
                this.radius = (int)Math.Min(colorRectangle.Width, colorRectangle.Height) / 2;
                this.centerPoint = colorRectangle.Location;
                this.centerPoint.Offset(radius, radius);

                // Start the pointer in the center.
                this.colorPoint = this.centerPoint;

                // Create angle region corresponding to the color circle.
                // Code uses this later to determine if angle specified
                // point is within the region, using the IsVisible
                // method.
                path.AddEllipse(colorRectangle);
                _colorRegion = new Region(path);

                // set { the range for the brightness selector.
                this.brightnessMin = this.brightnessRectangle.Top;
                this.brightnessMax = this.brightnessRectangle.Bottom;

                // Create angle region corresponding to the
                // brightness rectangle, with angle little extra
                // "breathing room".

                path.AddRectangle(new Rectangle(brightnessRectangle.Left, brightnessRectangle.Top - 10, brightnessRectangle.Width + 10, brightnessRectangle.Height + 20));
                // Create region corresponding to brightness
                // rectangle. Later code uses this to
                // determine if angle specified point is within
                // the region, using the IsVisible method.
                _brightnessRegion = new Region(path);

                // Set the location for the brightness indicator "marker".
                // Also calculate the scaling factor, scaling the height
                // to be between 0 and 255.
                brightnessX = brightnessRectangle.Left + brightnessRectangle.Width;
                brightnessScaling = (double)255 / (brightnessMax - brightnessMin);

                // Calculate the location of the brightness
                // pointer. Assume it's at the highest position.
                brightnessPoint = new Point(brightnessX, brightnessMax);

                // Create the bitmap that contains the circular gradient.
                CreateGradient();

                CalcCoordsAndUpdate(default_color);
            }
        }
Beispiel #12
0
        private void SetHSV( DRColor.HSV HSV)
        {
            if (HSV == null)
                return;

            // Update the HSV values on the form.
            RefreshValue(hsbHue, HSV.Hue);
            RefreshValue(hsbSaturation, HSV.Saturation);
            RefreshValue(hsbBrightness, HSV.Value);
            SetHSVLabels(HSV);
        }
Beispiel #13
0
 public void Draw(Graphics g, DRColor.HSV HSV)
 {
     // Given HSV values, update the screen.
     this._g = g;
     this.HSV = HSV;
     CalcCoordsAndUpdate(this.HSV);
     UpdateDisplay();
 }
Beispiel #14
0
 protected void OnColorChanged(DRColor.RGB RGB, DRColor.HSV HSV)
 {
     ColorChangedEventArgs e = new ColorChangedEventArgs(RGB, HSV);
     ColorChanged(this, e);
 }
Beispiel #15
0
 public static DRColor.RGB equalMix(DRColor.RGB a, DRColor.RGB b)
 {
     return new DRColor.RGB((a.Red + b.Red) / 2, (a.Green + b.Green) / 2, (a.Blue + b.Blue) / 2);
 }
Beispiel #16
0
 public static DRColor.RGB addHighest(DRColor.RGB a, DRColor.RGB b)
 {
     return new DRColor.RGB(Math.Max(b.Red, a.Red), Math.Max(b.Green, a.Green), Math.Max(b.Blue, a.Blue));
 }
Beispiel #17
0
        public void Draw(Graphics g, Point mousePoint)
        {
            // You've moved the mouse.
            // Now update the screen to match.

            double distance;
            int    degrees;
            Point  delta;
            Point  newColorPoint;
            Point  newBrightnessPoint;
            Point  newPoint;

            // Keep track of the previous color pointer point,
            // so you can put the mouse there in case the
            // user has clicked outside the circle.
            newColorPoint      = colorPoint;
            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.
                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;
                RGB = new DRColor.RGB(HSV);
                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.
                delta = new Point(
                    mousePoint.X - centerPoint.X, mousePoint.Y - centerPoint.Y);
                degrees = CalcDegrees(delta);

                // Calculate distance from the center to the new point
                // as angle fraction of the radius_norm. Use your old friend,
                // the Pythagorean theorem, to calculate this value.
                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        = (int)(degrees * 255 / 360);
                HSV.Saturation = (int)(distance * 255);
                HSV.Value      = brightness;
                RGB            = new DRColor.RGB(HSV);
                fullColor      = DRColor.HSVtoColor(new DRColor.HSV(HSV.Hue, HSV.Saturation, 255));
                break;
            }
            selectedColor = DRColor.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.


            // 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();
            OnColorChanged(RGB, HSV);
        }
Beispiel #18
0
 // a * perc + b * (1- perc)
 public static DRColor.RGB variableMix(DRColor.RGB a, DRColor.RGB b, double perc)
 {
     return new DRColor.RGB((int)(a.Red * perc + b.Red * (1 - perc)), (int)(a.Green * perc + b.Green * (1 - perc)), (int)(a.Blue * perc + b.Blue * (1 - perc)));
 }
Beispiel #19
0
 private void SetRGBLabels(DRColor.RGB RGB)
 {
     RefreshText(lblRed, RGB.Red);
     RefreshText(lblBlue, RGB.Blue);
     RefreshText(lblGreen, RGB.Green);
 }
Beispiel #20
0
 private void SetRGB(DRColor.RGB RGB)
 {
     if (RGB == null)
         return;
     // Update the RGB values on the form.
     RefreshValue(hsbRed, RGB.Red);
     RefreshValue(hsbBlue, RGB.Blue);
     RefreshValue(hsbGreen, RGB.Green);
     SetRGBLabels(RGB);
 }
Beispiel #21
0
 private void SetHSVLabels(DRColor.HSV HSV)
 {
     RefreshText(lblHue, HSV.Hue);
     RefreshText(lblSaturation, HSV.Saturation);
     RefreshText(lblBrightness, HSV.Value);
 }
Beispiel #22
0
 /*
 *  Used to push LED changes to strip.
 */
 private static bool update(DRColor.RGB[] main, DRColor.RGB[] buffer, int length, int strip)
 {
     bool hasChanged = false;
     for (int i = 0; i < length; i++)
     {
         if (main[i].different(buffer[i]))
         {
             hasChanged = true;
             sendClr(i, main[i], strip);
         }
     }
     Array.Copy(main, buffer, length);
     return hasChanged;
 }
Beispiel #23
0
 public static DRColor.RGB increaseBrightness(DRColor.RGB rgb, int amt)
 {
     DRColor.HSV hsv = new DRColor.HSV(rgb);
     hsv.Value = hsv.Value + amt > 256 ? 256 : hsv.Value + amt;
     return new DRColor.RGB(hsv);
 }
 private DRColor.RGB AdjustVal(DRColor.RGB rgb, double perc)
 {
     DRColor.HSV hsv = new DRColor.HSV(rgb);
     hsv.Value = (int)(hsv.Value * perc);
     return new DRColor.RGB(hsv);
 }
Beispiel #25
0
            public RGB(DRColor.HSV hsv)
            {
                if (hsv == null)
                {
                    _red = 0;
                    _green = 0;
                    _blue = 0;
                    return;
                }

                // HSV contains values scaled as in the color wheel:
                // that is, all from 0 to 128.

                // for ( this code to work, HSV.Hue needs
                // to be scaled from 0 to 360 (it//s the angle of the selected
                // point within the circle). HSV.Saturation and HSV.value must be
                // scaled to be between 0 and 1.

                double h;
                double s;
                double v;

                double r = 0;
                double g = 0;
                double b = 0;

                // Scale Hue to be between 0 and 360. Saturation
                // and value scale to be between 0 and 1.
                h = ((double)hsv.Hue / 255 * 360) % 360;
                s = (double)hsv.Saturation / 255;
                v = (double)hsv.Value / 255;

                if (s == 0)
                {
                    // If s is 0, all colors are the same.
                    // This is some flavor of gray.
                    r = v;
                    g = v;
                    b = v;
                }
                else
                {
                    double p;
                    double q;
                    double t;

                    double fractionalSector;
                    int sectorNumber;
                    double sectorPos;

                    // The color wheel consists of 6 sectors.
                    // Figure out which sector you//re in.
                    sectorPos = h / 60;
                    sectorNumber = (int)(Math.Floor(sectorPos));

                    // get the fractional part of the sector.
                    // That is, how many degrees into the sector
                    // are you?
                    fractionalSector = sectorPos - sectorNumber;

                    // Calculate values for the three axes
                    // of the color.
                    p = v * (1 - s);
                    q = v * (1 - (s * fractionalSector));
                    t = v * (1 - (s * (1 - fractionalSector)));

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

                        case 1:
                            r = q;
                            g = v;
                            b = p;
                            break;

                        case 2:
                            r = p;
                            g = v;
                            b = t;
                            break;

                        case 3:
                            r = p;
                            g = q;
                            b = v;
                            break;

                        case 4:
                            r = t;
                            g = p;
                            b = v;
                            break;

                        case 5:
                            r = v;
                            g = p;
                            b = q;
                            break;
                    }
                }
                // return an RGB structure, with values scaled
                // to be between 0 and 255.
                this.setRed((int)(r * 128));
                this.setGreen((int)(g * 128));
                this.setBlue((int)(b * 128));
            }
Beispiel #26
0
 public void Draw(Graphics g, DRColor.RGB RGB)
 {
     // Given RGB values, calculate HSV and then update the screen.
     this._g = g;
     this.HSV = new DRColor.HSV(RGB);
     CalcCoordsAndUpdate(this.HSV);
     UpdateDisplay();
 }
Beispiel #27
0
            public HSV(DRColor.RGB rgb)
            {
                // In this function, R, G, and B values must be scaled
                // to be between 0 and 1.
                // HSV.Hue will be angle value between 0 and 360, and
                // HSV.Saturation and value are between 0 and 1.
                // The code must scale these to be between 0 and 255 for
                // the purposes of this application.
                double min;
                double max;
                double delta;

                double r = (double)rgb.Red / 128;
                double g = (double)rgb.Green/ 128;
                double b = (double)rgb.Blue / 128;

                double h;
                double s;
                double v;

                min = Math.Min(Math.Min(r, g), b);
                max = Math.Max(Math.Max(r, g), b);
                v = max;
                delta = max - min;
                if (max == 0 || delta == 0)
                {
                    // R, G, and B must be 0, or all the same.
                    // In this case, S is 0, and H is undefined.
                    // Using H = 0 is as good as any...
                    s = 0;
                    h = 0;
                }
                else
                {
                    s = delta / max;
                    if (r == max)
                    {
                        // Between Yellow and Magenta
                        h = (g - b) / delta;
                    }
                    else if (g == max)
                    {
                        // Between Cyan and Yellow
                        h = 2 + (b - r) / delta;
                    }
                    else
                    {
                        // Between Magenta and Cyan
                        h = 4 + (r - g) / delta;
                    }

                }
                // Scale height to be between 0 and 360.
                // This may require adding 360, if the value
                // is negative.
                h *= 60;
                if (h < 0)
                {
                    h += 360;
                }

                // Scale to the requirements of this
                // application. All values are between 0 and 255.
                Hue = (int)(h / 360 * 255);
                Saturation = (int)(s * 255);
                Value = (int)(v * 255);
            }
Beispiel #28
0
        private void CalcCoordsAndUpdate(DRColor.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_norm.
            // 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 = DRColor.HSVtoColor(HSV);
            RGB = new DRColor.RGB(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 = DRColor.HSVtoColor(new DRColor.HSV(HSV.Hue, HSV.Saturation, 255));
        }
Beispiel #29
0
 // Gurantees position isn't out of bounds
 public static void KaiSet(int pos, DRColor.RGB rgb)
 {
     posClamp(ref pos, 1);
     _kai[pos] = rgb;
 }
Beispiel #30
0
 public MoodSeq(DRColor.RGB rgb)
 {
     Color_List.Add(rgb);
 }
 public ColorChangedEventArgs(DRColor.RGB RGB, DRColor.HSV HSV)
 {
     mRGB = RGB;
     mHSV = HSV;
 }