Esempio n. 1
0
        public static Color FromHSB(HSBColor hsbColor)
        {
            float r = hsbColor.b;
            float g = hsbColor.b;
            float b = hsbColor.b;
            if (hsbColor.s != 0)
            {
                float max = hsbColor.b;
                float dif = hsbColor.b * hsbColor.s / 255f;
                float min = hsbColor.b - dif;

                float h = hsbColor.h * 360f / 255f;

                if (h < 60f)
                {
                    r = max;
                    g = h * dif / 60f + min;
                    b = min;
                }
                else if (h < 120f)
                {
                    r = -(h - 120f) * dif / 60f + min;
                    g = max;
                    b = min;
                }
                else if (h < 180f)
                {
                    r = min;
                    g = max;
                    b = (h - 120f) * dif / 60f + min;
                }
                else if (h < 240f)
                {
                    r = min;
                    g = -(h - 240f) * dif / 60f + min;
                    b = max;
                }
                else if (h < 300f)
                {
                    r = (h - 240f) * dif / 60f + min;
                    g = min;
                    b = max;
                }
                else if (h <= 360f)
                {
                    r = max;
                    g = min;
                    b = -(h - 360f) * dif / 60 + min;
                }
                else
                {
                    r = 0;
                    g = 0;
                    b = 0;
                }
            }

            return Color.FromArgb(hsbColor.a,
                (int)Math.Round(Math.Min(Math.Max(r, 0), 255)),
                (int)Math.Round(Math.Min(Math.Max(g, 0), 255)),
                (int)Math.Round(Math.Min(Math.Max(b, 0), 255)));
        }
Esempio n. 2
0
        public static HSBColor FromColor(Color color)
        {
            HSBColor ret = new HSBColor(0f, 0f, 0f);
            ret.a = color.A;

            float r = color.R;
            float g = color.G;
            float b = color.B;

            float max = Math.Max(r, Math.Max(g, b));
            if (max <= 0)
                return ret;

            float min = Math.Min(r, Math.Min(g, b));
            float dif = max - min;

            if (max > min)
            {
                if (g == max)
                {
                    ret.h = (b - r) / dif * 60f + 120f;
                }
                else if (b == max)
                {
                    ret.h = (r - g) / dif * 60f + 240f;
                }
                else if (b > g)
                {
                    ret.h = (g - b) / dif * 60f + 360f;
                }
                else
                {
                    ret.h = (g - b) / dif * 60f;
                }
                if (ret.h < 0)
                {
                    ret.h = ret.h + 360f;
                }
            }
            else
            {
                ret.h = 0;
            }

            ret.h *= 255f / 360f;
            ret.s = (dif / max) * 255f;
            ret.b = max;

            return ret;
        }
Esempio n. 3
0
        public static Brush GetDraggedBrush(Brush brush)
        {
            // extract the color
            Color clr = new Pen(brush).Color;

            // generate the hsb color
            HSBColor hsb = new HSBColor(clr);

            // generate the dragged color
            HSBColor draggedhsb = new HSBColor(hsb.A, hsb.H, hsb.S, hsb.B - 10.0f);

            // check if we got a solid brush so we also return one
            SolidBrush sb = brush as SolidBrush;

            if (sb != null)
            { return new SolidBrush(draggedhsb.Color); }

            // unhandled brush type
            Debug.Check(false);

            // if the brush type was not handled in release mode we return a solid brush by default
            return new SolidBrush(draggedhsb.Color);
        }
Esempio n. 4
0
        public static Color FromHSB(HSBColor hsbColor)
        {
            float r = hsbColor.b;
            float g = hsbColor.b;
            float b = hsbColor.b;

            if (hsbColor.s != 0)
            {
                float max = hsbColor.b;
                float dif = hsbColor.b * hsbColor.s / 255f;
                float min = hsbColor.b - dif;

                float h = hsbColor.h * 360f / 255f;

                if (h < 60f)
                {
                    r = max;
                    g = h * dif / 60f + min;
                    b = min;
                }
                else if (h < 120f)
                {
                    r = -(h - 120f) * dif / 60f + min;
                    g = max;
                    b = min;
                }
                else if (h < 180f)
                {
                    r = min;
                    g = max;
                    b = (h - 120f) * dif / 60f + min;
                }
                else if (h < 240f)
                {
                    r = min;
                    g = -(h - 240f) * dif / 60f + min;
                    b = max;
                }
                else if (h < 300f)
                {
                    r = (h - 240f) * dif / 60f + min;
                    g = min;
                    b = max;
                }
                else if (h <= 360f)
                {
                    r = max;
                    g = min;
                    b = -(h - 360f) * dif / 60 + min;
                }
                else
                {
                    r = 0;
                    g = 0;
                    b = 0;
                }
            }

            return(Color.FromArgb(hsbColor.a,
                                  (int)Math.Round(Math.Min(Math.Max(r, 0), 255)),
                                  (int)Math.Round(Math.Min(Math.Max(g, 0), 255)),
                                  (int)Math.Round(Math.Min(Math.Max(b, 0), 255))));
        }
Esempio n. 5
0
        public static Color FromHSB(HSBColor hsbColor)
        {
            float red   = hsbColor.brightness;
            float green = hsbColor.brightness;
            float blue  = hsbColor.brightness;

            if (hsbColor.saturation != 0)
            {
                float max = hsbColor.brightness;
                float dif = hsbColor.brightness * hsbColor.saturation / 255f;
                float min = hsbColor.brightness - dif;

                float h = hsbColor.hue * 360f / 255f;

                if (h < 60f)
                {
                    red   = max;
                    green = h * dif / 60f + min;
                    blue  = min;
                }
                else if (h < 120f)
                {
                    red   = -(h - 120f) * dif / 60f + min;
                    green = max;
                    blue  = min;
                }
                else if (h < 180f)
                {
                    red   = min;
                    green = max;
                    blue  = (h - 120f) * dif / 60f + min;
                }
                else if (h < 240f)
                {
                    red   = min;
                    green = -(h - 240f) * dif / 60f + min;
                    blue  = max;
                }
                else if (h < 300f)
                {
                    red   = (h - 240f) * dif / 60f + min;
                    green = min;
                    blue  = max;
                }
                else if (h <= 360f)
                {
                    red   = max;
                    green = min;
                    blue  = -(h - 360f) * dif / 60 + min;
                }
                else
                {
                    red   = 0;
                    green = 0;
                    blue  = 0;
                }
            }

            return(Color.FromArgb(hsbColor.alpha, (int)Math.Round(Math.Min(Math.Max(red, 0), 255)), (int)Math.Round(Math.Min(Math.Max(green, 0), 255)),
                                  (int)Math.Round(Math.Min(Math.Max(blue, 0), 255))));
        }