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

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

c# - FromArgb vs FromScRgb

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...