locked
How to set color for a BoxView ? RRS feed

  • Question

  • User231685 posted

    Hi there !

    I'm trying to set a dynamic color for my BoxView, but the refresh taking a long time... And the result doesn't show all the colors I want.

    I would like to create a animation who change color of RGB (red -> -> blue -> ... -> yellow -> red (repeat)) smoothly.

    The algorithm work good, but the bv_filter.Color = Color.FromRgb(r, g, b); doesn't want to refresh all times.

    Here is my code : `public StartPage() { InitializeComponent();

            this.Appearing += async (object sender, EventArgs e) =>
            {
                while (true)
                {
                    await change_color(10);    
                }
            };
        }
    
        private async Task<bool> change_color(Int32 delay)
        {
            double r = 255;
            double g = 0;
            double b = 0;
    
            while (r != 0 | g != 0 | b != 0)
            {
                if (r == 255 & g == 0)
                    b ++;
    
                if (b == 255 & g == 0)
                    r --;
    
                if (b == 255 & r == 0)
                    g ++;
    
                if (g == 255 & r == 0)
                    b --;
    
                if (g == 255 & b == 0)
                    r ++;
    
                if (r == 255 & b == 0)
                    g --;
    
                // Debug.WriteLine(r + "," + g + "," + b);
                bv_filter.Color = Color.FromRgb(r, g, b);
    
                await Task.Delay(delay);
    
            }
    
            return true;
        }`
    

    I'm looking for the PropertyChanged, but I doesn't find answer... :/

    I'm newbie in Xamarin, thanks in advance for anyone who can give me some help :)

    Sorry for my bad english...

    Tuesday, June 7, 2016 8:01 PM

Answers

  • User231685 posted

    Shame on me !

    The Color.FromRgb(r, g, b); accept only values [0-1] and not [0-255] as one might think !

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, June 7, 2016 8:47 PM

All replies

  • User231685 posted

    Shame on me !

    The Color.FromRgb(r, g, b); accept only values [0-1] and not [0-255] as one might think !

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, June 7, 2016 8:47 PM
  • User366212 posted

    Hi,

    According to this: docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/colors The values for Color.FromRgb(r, g, b);are [0-255] but it does not work, as you wrote :( Can you please add here your solution with the values [0-1]?

    Thanks!

    Wednesday, July 18, 2018 5:04 AM
  • User89714 posted

    There are two forms of Color.FromRgb, as follows. If you are having problems with the second of these, check that the arguments you specify are doubles (or floats, which would get converted to doubles) in the range 0.0 - 1.0

    public static Color FromRgb(int r, int g, int b); public static Color FromRgb(double r, double g, double b);

    Wednesday, July 18, 2018 8:34 AM