Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Should these two expressions result in colors which are roughly the same?

Color.FromArgb(255, 255, 255, (byte)0.25 * 255))

Color.FromScRgb(1.0f, 1.0f, 1.0f, 0.25f))

This test program demonstrates that they show up with seemingly different alpha values:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Test_FromArgb_FromScRbg
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var panel = new StackPanel();

            Content = panel;

            panel.Children.Add(
                new Rectangle()
                {
                    Width = 100,
                    Height = 100,
                    Fill = new SolidColorBrush(
                        Color.FromArgb(
                            255, 
                            255, 
                            255, 
                            (byte)0.25 * 255))
                });

            panel.Children.Add(
                new Rectangle()
                {
                    Width = 100,
                    Height = 100,
                    Fill = new SolidColorBrush(
                        Color.FromScRgb(
                            1.0f,
                            1.0f,
                            1.0f,
                            0.25f))
                });
        }
    }
}

Here's what the demo program looks like on my system:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
365 views
Welcome To Ask or Share your Answers For Others

1 Answer

The basic reason behind this is mentioned by mzzzzb above:

the scale 0.0 - 1.0 does not map linearly to 0 - 255

I.e. the ScRgb components do not map linearly to the Argb components.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...