Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent colors from blending?

Tags:

c#

.net

wpf

Consider the following example:

<Grid  HorizontalAlignment="Stretch" VerticalAlignment="Top">
    <Line Stroke="Red" X1="0" X2="100" Y1="50" Y2="50"/>
    <Line Stroke="Red" X1="50" X2="50" Y1="0" Y2="100"/>
</Grid>

Despite the fact that both lines have SolidColorBrush and both have opacity=1, a color blending still occurs: The pixel at the point of intersection is of darker red color.

Why does it happen and how can I prevent it?

Thanks!

P.S Here is another example of the same code with the brushes explicitly defined:

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Top">
    <Grid.Resources>
        <Style TargetType="{x:Type Line}">
            <Setter Property="Stroke">
                <Setter.Value>
                    <SolidColorBrush Color="Red" Opacity="1" />
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>

    <Line X1="0" X2="100" Y1="50" Y2="50" />
    <Line X1="50" X2="50" Y1="0" Y2="100" />
</Grid>
like image 201
VitalyB Avatar asked May 12 '11 08:05

VitalyB


People also ask

What colors should you not mix?

The three primary colors are red, yellow, and blue; they are the only colors that cannot be made by mixing two other colors.

What is it called when colors are blended together?

Ombré /ˈɒmbreɪ/ (literally "shaded" in French) is the blending of one color hue to another, usually moving tints and shades from light to dark.


1 Answers

You might want to look into snapping to pixels. This is a feature of WPF that allows for some extra granularity in rendering. Say if we take a Line and apply this:

<Line X1="0" X2="100" Y1="50" Y2="50" SnapsToDevicePixels="True"/>

As an example of what this serves to do, here's an image from the linked article:

enter image description here

like image 50
Grant Thomas Avatar answered Oct 01 '22 11:10

Grant Thomas