Search code examples
colorswindows-runtimefractalswindows-store-apps

Color.HSBtoRGB missing in WinRT


I'm builing a fractal application and need to generate a smooth color scheme, and I found a nice algorithm at Smooth spectrum for Mandelbrot Set rendering.

But that required me to call Color.HSBtoRGB and that method is not available in WinRT / Windows Store apps.

Is there some other built-in method to do this conversion? Other tips on how to convert HSB to RGB?


Solution

  • I ended up using the HSB to RGB conversion algorithm found at http://www.adafruit.com/blog/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/, I adopted the inital (long) version. Perhaps this can be further optimized but for my purpose this was perfect!

    As the hsb2rgb method is in C and I needed C#, I'm sharing my version here:

        private byte[] hsb2rgb(int index, byte sat, byte bright)
        {
            int r_temp, g_temp, b_temp;
            byte index_mod;
            byte inverse_sat = (byte)(sat ^ 255);
    
            index = index % 768;
            index_mod = (byte)(index % 256);
    
            if (index < 256)
            {
                r_temp = index_mod ^ 255;
                g_temp = index_mod;
                b_temp = 0;
            }
            else if (index < 512)
            {
                r_temp = 0;
                g_temp = index_mod ^ 255;
                b_temp = index_mod;
            }
    
            else if ( index < 768)
            {
                r_temp = index_mod;
                g_temp = 0;
                b_temp = index_mod ^ 255;
            }
    
            else
            {
                r_temp = 0;
                g_temp = 0;
                b_temp = 0;
            }
    
            r_temp = ((r_temp * sat) / 255) + inverse_sat;
            g_temp = ((g_temp * sat) / 255) + inverse_sat;
            b_temp = ((b_temp * sat) / 255) + inverse_sat;
    
            r_temp = (r_temp * bright) / 255;
            g_temp = (g_temp * bright) / 255;
            b_temp = (b_temp * bright) / 255;
    
            byte[] color = new byte[3];
            color[0]    = (byte)r_temp;
            color[1]    = (byte)g_temp;
            color[2]    = (byte)b_temp;
    
            return color;
        }
    

    To call it based on the code linked in the original post I needed to make some minor modifications:

        private byte[] SmoothColors1(int maxIterationCount, ref Complex z, int iteration)
        {
            double smoothcolor = iteration + 1 - Math.Log(Math.Log(z.Magnitude)) / Math.Log(2);
            byte[] color = hsb2rgb((int)(10 * smoothcolor), (byte)(255 * 0.6f), (byte)(255 * 1.0f));
    
            if (iteration >= maxIterationCount)
            {
                // Make sure the core is black
                color[0] = 0;
                color[1] = 0;
                color[2] = 0;
            }
            return color;
        }