Joy Rex
Joy Rex

Reputation: 618

Xamarin Forms converter for Android Color Brightness is not proper

In Xamarin Forms for Android, i have used the below method to convert Xamarin.Forms.Color to Android.Graphics.Color

public static Android.Graphics.Color GetRGB(float hue, float saturation, float luminosity)
        {
            int r = 0, g = 0, b = 0;
            if (saturation == 0)
            {
                r = g = b = (int)(luminosity * 255.0f + 0.5f);
            }
            else
            {
                float h = (hue - (float)Math.Floor(hue)) * 6.0f;
                float f = h - (float)Math.Floor(h);
                float p = luminosity * (1.0f - saturation);
                float q = luminosity * (1.0f - saturation * f);
                float t = luminosity * (1.0f - (saturation * (1.0f - f)));
                switch ((int)h)
                {
                    case 0:
                        r = (int)(luminosity * 255.0f + 0.5f);
                        g = (int)(t * 255.0f + 0.5f);
                        b = (int)(p * 255.0f + 0.5f);
                        break;
                    case 1:
                        r = (int)(q * 255.0f + 0.5f);
                        g = (int)(luminosity * 255.0f + 0.5f);
                        b = (int)(p * 255.0f + 0.5f);
                        break;
                    case 2:
                        r = (int)(p * 255.0f + 0.5f);
                        g = (int)(luminosity * 255.0f + 0.5f);
                        b = (int)(t * 255.0f + 0.5f);
                        break;
                    case 3:
                        r = (int)(p * 255.0f + 0.5f);
                        g = (int)(q * 255.0f + 0.5f);
                        b = (int)(luminosity * 255.0f + 0.5f);
                        break;
                    case 4:
                        r = (int)(t * 255.0f + 0.5f);
                        g = (int)(p * 255.0f + 0.5f);
                        b = (int)(luminosity * 255.0f + 0.5f);
                        break;
                    case 5:
                        r = (int)(luminosity * 255.0f + 0.5f);
                        g = (int)(p * 255.0f + 0.5f);
                        b = (int)(q * 255.0f + 0.5f);
                        break;
                }
            }

            Android.Graphics.Color color = new Android.Graphics.Color(r, g, b);

            return color;
        }

the returned color value is darker than the original, i have checked its alpha value but its 255 only.

What am i missing?

Upvotes: 0

Views: 533

Answers (1)

dmunch
dmunch

Reputation: 394

You got an extension method ToAndroid() on Xamarin.Forms.Color which does the work for you. Just add a using Xamarin.Forms.Platform.Android; and it should be available.

Upvotes: 2

Related Questions