Question

Okay I am trying to follow this guide to making a converter in c#: http://www.geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm

here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace ColorPickerTest
{

      // Given a Color (RGB Struct) in range of 0-255
      // Return H,S,L in range of 0-1

    class Program
    {


        public struct ColorRGB
        {
            public byte R;
            public byte G;
            public byte B;
            public ColorRGB(Color value)
            {
                this.R = value.R;
                this.G = value.G;
                this.B = value.B;
            }
            public static implicit operator Color(ColorRGB rgb)
            {
                Color c = Color.FromArgb(rgb.R, rgb.G, rgb.B);
                return c;
            }
            public static explicit operator ColorRGB(Color c)
            {
                return new ColorRGB(c);
            }
        }
        public static void RGB2HSL(ColorPickerTest.Program.ColorRGB rgb, out double h, out double s, out double l)
        {
            double r = rgb.R / 255.0;
            double g = rgb.G / 255.0;
            double b = rgb.B / 255.0;
            double v;
            double m;
            double vm;
            double r2, g2, b2;

            h = 0; // default to black
            s = 0;
            l = 0;
            v = Math.Max(r, g);
            v = Math.Max(v, b);
            m = Math.Min(r, g);
            m = Math.Min(m, b);
            l = (m + v) / 2.0;
            if (l <= 0.0)
            {
                return;
            }
            vm = v - m;
            s = vm;
            if (s > 0.0)
            {
                s /= (l <= 0.5) ? (v + m) : (2.0 - v - m);
            }
            else
            {
                return;
            }
            r2 = (v - r) / vm;
            g2 = (v - g) / vm;
            b2 = (v - b) / vm;
            if (r == v)
            {
                h = (g == m ? 5.0 + b2 : 1.0 - g2);
            }
            else if (g == v)
            {
                h = (b == m ? 1.0 + r2 : 3.0 - b2);
            }
            else
            {
                h = (r == m ? 3.0 + g2 : 5.0 - r2);
            }
            h /= 6.0;
        }


        static void Main(string[] args)
        {

            Color slateBlue = Color.FromName("SlateBlue");
            byte g = slateBlue.G;
            byte b = slateBlue.B;
            byte r = slateBlue.R;
            byte a = slateBlue.A;
            string text = String.Format("Slate Blue has these ARGB values: Alpha:{0}, " +
                "red:{1}, green: {2}, blue {3}", new object[]{a, r, g, b});
            Console.WriteLine(text);
            Console.ReadKey();


            ColorPickerTest.Program.ColorRGB rgb = new  ColorPickerTest.Program.ColorRGB(slateBlue);
            double h, s, l;
            RGB2HSL(rgb, h, s, l);

        }
    }
}

I am getting an error when i call the final method: 'RGB2HSL', saying the method has invalid arguments.

Not sure if it is to do with my overloaded struct for ColorRGB, or if I am meant to be qualifying it

Was it helpful?

Solution

You just forgot the word out into the function calling

RGB2HSL(rgb, out h, out s, out l);

Let me know if this solved your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top