none
Converting Colors to Decimal

    Question

  • Hi There

    I Have a strange question, so i hope someone out there can help me.
    I am using C# and XAML, so the first two characters of the following Hex values are the Alpha(Opacity).

    If  0° = Red (FFFF0000) and 360°=Green (FF00FF00), would there be any way of calculating what the color would be at any degree in betwee those values?

    Thank you in advance
    Dan
    Monday, September 01, 2008 2:46 PM

Answers

  • Your title says "Converting Color to Decimal", I think you mean the other way around.

    First, just so that it's said, 0 degrees and 360 degrees are usually treated as being equal because they fall on the same location on a circle.

    To convert a decimal between the values of 0 and 360, to a color, you have to decide on the effect you want.  There is no "standard" for taking degrees or radian an giving them a color except perhaps in a color wheel (but then 0 and 360 would both have the same color).

    Here's an idea you can adapt.
            int DegreeToColor(int degree)
            {
                int A, R, G, B;
                if(degree < 0 || degree > 360)
                    return 0;

                A = 0xff;
                R = DegreeToR(degree);
                G = DegreeToG(degree);
                B = 0;
                return Color.FromArgb(A,R,G,B).ToArgb();
            }

            // Adjust the functions to do what you like, these are both linear
            int DegreeToG(int degree) { return (256 * degree) / 361; }
            int DegreeToR(int degree) { return 255 - (256 * degree) / 361; }


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Marked as answer by The_D_Man Monday, September 01, 2008 3:31 PM
    • Marked as answer by The_D_Man Monday, September 01, 2008 3:31 PM
    • Marked as answer by The_D_Man Monday, September 01, 2008 3:31 PM
    Monday, September 01, 2008 3:18 PM
  • One approach is a linear rise in GREEN from 0 to 180, followed by a linear fall in RED from 181 to 360...  Here's a possibility.  Again, tweak to get the effect you want, but test your boundaries, your functions should never return negative or greater than 255.


             int DegreeToG(int degree)
            {
                int p = (256 * degree) / 361;
                if (degree <= 180)
                    p = (255 * degree) / 180;
                else
                    p = 255;
                return p;
            }
            int DegreeToR(int degree)
            {
                int p = 255 - (256 * degree) / 361;
                if (degree <= 180)
                    p = 255;
                else
                    p = 255 - (255 * (degree - 180)) / 180;
                return p;
            }

    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Marked as answer by The_D_Man Tuesday, September 02, 2008 8:12 AM
    Monday, September 01, 2008 6:13 PM

All replies

  • Your title says "Converting Color to Decimal", I think you mean the other way around.

    First, just so that it's said, 0 degrees and 360 degrees are usually treated as being equal because they fall on the same location on a circle.

    To convert a decimal between the values of 0 and 360, to a color, you have to decide on the effect you want.  There is no "standard" for taking degrees or radian an giving them a color except perhaps in a color wheel (but then 0 and 360 would both have the same color).

    Here's an idea you can adapt.
            int DegreeToColor(int degree)
            {
                int A, R, G, B;
                if(degree < 0 || degree > 360)
                    return 0;

                A = 0xff;
                R = DegreeToR(degree);
                G = DegreeToG(degree);
                B = 0;
                return Color.FromArgb(A,R,G,B).ToArgb();
            }

            // Adjust the functions to do what you like, these are both linear
            int DegreeToG(int degree) { return (256 * degree) / 361; }
            int DegreeToR(int degree) { return 255 - (256 * degree) / 361; }


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Marked as answer by The_D_Man Monday, September 01, 2008 3:31 PM
    • Marked as answer by The_D_Man Monday, September 01, 2008 3:31 PM
    • Marked as answer by The_D_Man Monday, September 01, 2008 3:31 PM
    Monday, September 01, 2008 3:18 PM
  • That answer worked perfectly.
    Thank you.

    I probably should have put more detail in my question. I have a KPI(Key Performance Indicator) that has a status ring in it, as my needle moves from 0° around my dial i need to change the color to point at which the needle stops. So if my status ring is red at 0° and green at 360° then at 180° it should be exactly Yellow, and every color throughout the degrees should only have a hue change and the brightness should be full. but with RGB the brightness is only half.
    I am more interested in the rotation angle of a needle rather than the radial position.

    I hope you can help me figure this one out.

    Thank you
    Dan
    Monday, September 01, 2008 3:52 PM
  • One approach is a linear rise in GREEN from 0 to 180, followed by a linear fall in RED from 181 to 360...  Here's a possibility.  Again, tweak to get the effect you want, but test your boundaries, your functions should never return negative or greater than 255.


             int DegreeToG(int degree)
            {
                int p = (256 * degree) / 361;
                if (degree <= 180)
                    p = (255 * degree) / 180;
                else
                    p = 255;
                return p;
            }
            int DegreeToR(int degree)
            {
                int p = 255 - (256 * degree) / 361;
                if (degree <= 180)
                    p = 255;
                else
                    p = 255 - (255 * (degree - 180)) / 180;
                return p;
            }

    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Marked as answer by The_D_Man Tuesday, September 02, 2008 8:12 AM
    Monday, September 01, 2008 6:13 PM
  • Thank you so much for all your help. Thats helped me alot. ;)

    Keep well
    Ciao
    Dan
    Tuesday, September 02, 2008 8:13 AM