LitDev.LDColours.HSLtoRGB C# (CSharp) Method

HSLtoRGB() public static method

Convert a Hue, Saturation, Lightness (HSL) color to a Red, Green, Blue (RGB) colour used by SmallBasic.
public static HSLtoRGB ( Primitive H, Primitive S, Primitive L ) : Primitive
H Primitive Hue (0 to 360).
S Primitive Saturation (0 to 1).
L Primitive Lightness (0 to 1).
return Primitive
        public static Primitive HSLtoRGB(Primitive H, Primitive S, Primitive L)
        {
            try
            {
                double[] RGB = HSL2RGB(H, S, L);
                return ColorTranslator.ToHtml(Color.FromArgb((int)(255 * RGB[0] + 0.5), (int)(255 * RGB[1] + 0.5), (int)(255 * RGB[2] + 0.5))).ToString();
            }
            catch (Exception ex)
            {
                Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                return "#000000";
            }
        }

Usage Example

Example #1
0
        public void Update()
        {
            Children.Clear();
            xc  = Width / 2.0;
            yc  = Height / 2.0;
            rad = scale * System.Math.Min(Width / 2.5, Height / 2.5);

            if (eLegend == Legends.LEGEND || eLegend == Legends.LEGEND_PERCENT)
            {
                rad *= 0.7;
                xc  -= 0.8 * (xc - rad);
            }

            double    angle = 0;
            double    x1 = rad, y1 = 0, x2, y2;
            Size      size = new Size(0, 0);
            Color     col  = Colors.White;
            Brush     brush;
            Ellipse   ellipse;
            Rectangle rectangle;
            TextBlock textblock;

            for (int i = 0; i < Count; i++)
            {
                double frac = startColor + (endColor - startColor) * i / (double)(Count);
                switch (eHSL)
                {
                case HSL.HUE:
                    col = (Color)ColorConverter.ConvertFromString(LDColours.HSLtoRGB(360 * frac, saturation, lightness));
                    break;

                case HSL.SATURATION:
                    col = (Color)ColorConverter.ConvertFromString(LDColours.HSLtoRGB(hue, frac, lightness));
                    break;

                case HSL.LIGHTNESS:
                    col = (Color)ColorConverter.ConvertFromString(LDColours.HSLtoRGB(hue, saturation, frac));
                    break;
                }
                brush = new SolidColorBrush(col);
                if (centralColour != "")
                {
                    GradientBrush gradientBrush = new GradientBrush("", new Primitive("1=" + centralColour + ";2=" + col.ToString() + ";"), "");
                    brush = gradientBrush.getBrush();
                }

                angle += 2 * System.Math.PI * Values[i] / Total;
                x2     = rad + rad * System.Math.Sin(angle);
                y2     = rad - rad * System.Math.Cos(angle);
                bool bLargeArc = (Values[i] / Total) > 0.5;

                switch (eStyle)
                {
                case Styles.PIE:
                {
                    PathSegmentCollection pathSegments = new PathSegmentCollection();
                    pathSegments.Add(new LineSegment(new Point(x1, y1), false));
                    pathSegments.Add(new ArcSegment(new Point(x2, y2), new Size(rad, rad), angle, bLargeArc, SweepDirection.Clockwise, false));
                    PathFigureCollection pathFigures = new PathFigureCollection();
                    pathFigures.Add(new PathFigure(new Point(rad, rad), pathSegments, true));
                    PathFigureCollection figCollection = new PathFigureCollection(pathFigures);

                    ellipse = new Ellipse {
                        Width = 2 * rad, Height = 2 * rad, Fill = brush
                    };
                    ellipse.Clip       = new PathGeometry(figCollection);
                    ellipse.Tag        = new Segment(xc, yc, rad, rad, Name, Labels[i]);
                    ellipse.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent);
                    Children.Add(ellipse);
                    Chart.SetLeft(ellipse, xc - rad);
                    Chart.SetTop(ellipse, yc - rad);
                }
                break;

                case Styles.DOUGHNUT:
                {
                    PathFigureCollection figCollection;
                    if (bDualDoughnut)
                    {
                        double x1inner = x1 + (1 - LDChart.DoughnutFraction) * (rad - x1);
                        double y1inner = y1 + (1 - LDChart.DoughnutFraction) * (rad - y1);
                        double x2inner = x2 + (1 - LDChart.DoughnutFraction) * (rad - x2);
                        double y2inner = y2 + (1 - LDChart.DoughnutFraction) * (rad - y2);
                        PathSegmentCollection pathSegments = new PathSegmentCollection();
                        pathSegments.Add(new LineSegment(new Point(x1, y1), false));
                        pathSegments.Add(new ArcSegment(new Point(x2, y2), new Size(rad, rad), angle, bLargeArc, SweepDirection.Clockwise, false));
                        pathSegments.Add(new LineSegment(new Point(x2inner, y2inner), false));
                        pathSegments.Add(new ArcSegment(new Point(x1inner, y1inner), new Size(rad * LDChart.DoughnutFraction, rad * LDChart.DoughnutFraction), angle, bLargeArc, SweepDirection.Counterclockwise, false));
                        PathFigureCollection pathFigures = new PathFigureCollection();
                        pathFigures.Add(new PathFigure(new Point(x1inner, y1inner), pathSegments, true));
                        figCollection = new PathFigureCollection(pathFigures);
                    }
                    else
                    {
                        PathSegmentCollection pathSegments = new PathSegmentCollection();
                        pathSegments.Add(new LineSegment(new Point(x1, y1), false));
                        pathSegments.Add(new ArcSegment(new Point(x2, y2), new Size(rad, rad), angle, bLargeArc, SweepDirection.Clockwise, false));
                        PathFigureCollection pathFigures = new PathFigureCollection();
                        pathFigures.Add(new PathFigure(new Point(rad, rad), pathSegments, true));
                        figCollection = new PathFigureCollection(pathFigures);
                    }

                    ellipse = new Ellipse {
                        Width = 2 * rad, Height = 2 * rad, Fill = brush
                    };
                    ellipse.Clip       = new PathGeometry(figCollection);
                    ellipse.Tag        = new Segment(xc, yc, rad, rad, Name, Labels[i]);
                    ellipse.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent);
                    Children.Add(ellipse);
                    Chart.SetLeft(ellipse, xc - rad);
                    Chart.SetTop(ellipse, yc - rad);
                }
                break;

                case Styles.BUBBLE:
                {
                    double sin = System.Math.Sin(System.Math.PI * Values[i] / Total);
                    double r   = rad * sin / (1.0 + sin);
                    angle -= System.Math.PI * Values[i] / Total;
                    double x = xc + (rad - r) * System.Math.Sin(angle);
                    double y = yc - (rad - r) * System.Math.Cos(angle);
                    angle  += System.Math.PI * Values[i] / Total;
                    ellipse = new Ellipse {
                        Width = 2 * r, Height = 2 * r, Fill = brush
                    };
                    ellipse.Tag        = new Segment(x, y, r, r, Name, Labels[i]);
                    ellipse.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent);
                    Children.Add(ellipse);
                    Chart.SetLeft(ellipse, x - r);
                    Chart.SetTop(ellipse, y - r);
                }
                break;

                case Styles.BAR:
                {
                    double w = rad * (Values[i] - Min) / (Max - Min);
                    double h = rad / (double)Count;
                    double x = xc - rad + w;
                    double y = yc - rad + (2 * i + 1) * h;
                    rectangle = new Rectangle {
                        Width = 2 * w, Height = 2 * h, Fill = brush
                    };
                    rectangle.Tag        = new Segment(x, y, w, h, Name, Labels[i]);
                    rectangle.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent);
                    Children.Add(rectangle);
                    Chart.SetLeft(rectangle, x - w);
                    Chart.SetTop(rectangle, y - h);
                }
                break;

                case Styles.COLUMN:
                {
                    double w = rad / (double)Count;
                    double h = rad * (Values[i] - Min) / (Max - Min);
                    double x = xc - rad + (2 * i + 1) * w;
                    double y = yc + rad - h;
                    rectangle = new Rectangle {
                        Width = 2 * w, Height = 2 * h, Fill = brush
                    };
                    rectangle.Tag        = new Segment(x, y, w, h, Name, Labels[i]);
                    rectangle.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent);
                    Children.Add(rectangle);
                    Chart.SetLeft(rectangle, x - w);
                    Chart.SetTop(rectangle, y - h);
                }
                break;
                }

                brush = new SolidColorBrush(col);
                x1    = x2;
                y1    = y2;

                if (eLegend == Legends.OVERLAY || eLegend == Legends.PERCENT || eLegend == Legends.LEGEND_PERCENT)
                {
                    textblock = new TextBlock
                    {
                        Text       = eLegend == Legends.OVERLAY ? Labels[i] : string.Format("{0:F1}%", 100 * Values[i] / Total),
                        Foreground = foreground,
                        FontFamily = fontFamily,
                        FontSize   = fontSize,
                        FontWeight = fontWeight,
                        FontStyle  = fontStyle
                    };
                    if (bLegendBackground)
                    {
                        textblock.Background = brush;
                    }
                    textblock.FontSize  *= legendScale;
                    textblock.MouseDown += new MouseButtonEventHandler(_ValueClickedEvent);
                    Children.Add(textblock);
                    textblock.Measure(size);
                    textblock.Arrange(new Rect(size));
                    angle -= System.Math.PI * Values[i] / Total;
                    double w = textblock.ActualWidth / 2.0;
                    double h = textblock.ActualHeight / 2.0;
                    if (eStyle == Styles.PIE)
                    {
                        x2 = xc + 0.67 * rad * System.Math.Sin(angle);
                        y2 = yc - 0.67 * rad * System.Math.Cos(angle);
                    }
                    else if (eStyle == Styles.DOUGHNUT)
                    {
                        x2 = xc + (1 + LDChart.DoughnutFraction) / 2.0 * rad * System.Math.Sin(angle);
                        y2 = yc - (1 + LDChart.DoughnutFraction) / 2.0 * rad * System.Math.Cos(angle);
                    }
                    else if (eStyle == Styles.BUBBLE)
                    {
                        double sin = System.Math.Sin(System.Math.PI * Values[i] / Total);
                        double r   = rad * sin / (1.0 + sin);
                        x2 = xc + (rad - r) * System.Math.Sin(angle);
                        y2 = yc - (rad - r) * System.Math.Cos(angle);
                    }
                    else if (eStyle == Styles.BAR)
                    {
                        //x2 = xc - rad + rad * (Values[i] - Min) / (Max - Min);
                        x2 = xc - rad + w + 5;
                        y2 = yc - rad + (2 * i + 1) * rad / (double)Count;
                    }
                    else if (eStyle == Styles.COLUMN)
                    {
                        x2 = xc - rad + (2 * i + 1) * rad / (double)Count;
                        //y2 = yc + rad - rad * (Values[i] - Min) / (Max - Min);
                        y2 = yc + rad - w - 5;
                        RotateTransform rotateTransform = new RotateTransform();
                        rotateTransform.CenterX   = w;
                        rotateTransform.CenterY   = h;
                        rotateTransform.Angle     = -90;
                        textblock.RenderTransform = rotateTransform;
                    }
                    angle        += System.Math.PI * Values[i] / Total;
                    textblock.Tag = new Segment(x2, y2, w, h, Name, Labels[i]);
                    Chart.SetLeft(textblock, x2 - w);
                    Chart.SetTop(textblock, y2 - h);
                    Canvas.SetZIndex(textblock, 1);
                }

                if (eLegend == Legends.LEGEND || eLegend == Legends.LEGEND_PERCENT)
                {
                    rectangle = new Rectangle {
                        Width = 10 * legendScale, Height = 10 * legendScale, Fill = brush
                    };
                    Children.Add(rectangle);
                    x2 = 2 * xc;
                    y2 = (Width - 15 * Count * legendScale) / 2 + 15 * i * legendScale;
                    Chart.SetLeft(rectangle, x2);
                    Chart.SetTop(rectangle, y2);
                    Canvas.SetZIndex(rectangle, 1);

                    textblock = new TextBlock
                    {
                        Text       = Labels[i],
                        Foreground = foreground,
                        FontFamily = fontFamily,
                        FontSize   = fontSize,
                        FontWeight = fontWeight,
                        FontStyle  = fontStyle
                    };
                    if (bLegendBackground)
                    {
                        textblock.Background = brush;
                    }
                    textblock.FontSize *= legendScale;
                    Children.Add(textblock);
                    textblock.Measure(size);
                    textblock.Arrange(new Rect(size));
                    Chart.SetLeft(textblock, x2 + 15 * legendScale);
                    Chart.SetTop(textblock, y2 + 5 * legendScale - textblock.ActualHeight / 2.0);
                    Canvas.SetZIndex(textblock, 1);
                }
            }

            if (eStyle == Styles.DOUGHNUT && !bDualDoughnut)
            {
                ellipse = new Ellipse {
                    Width = 2 * LDChart.DoughnutFraction * rad, Height = 2 * LDChart.DoughnutFraction * rad, Fill = Background
                };
                Children.Add(ellipse);
                Chart.SetLeft(ellipse, xc - LDChart.DoughnutFraction * rad);
                Chart.SetTop(ellipse, yc - LDChart.DoughnutFraction * rad);
            }
        }