Search code examples
c#wpflinear-gradientsvisualbrush

Get the color at an offset in a Rectange with a linear gradient


I have a Rectangle with the following XAML:

<Rectangle x:Name="ActiveIndex" Width="100" Height="15" Margin="50,165,50,20">
    <Rectangle.Fill>
        <LinearGradientBrush>
            <GradientStop Color="#6FFDFD" Offset="0.0" />
            <GradientStop Color="#0D00F9" Offset="1.0" />                    
        </LinearGradientBrush>
    </Rectangle.Fill>
</Rectangle>

I need a code solution which when given the Input Offset from 1 to 100 can find the Color from ActiveIndex.

Currently I am using an another bound Rectangle with a Viewbox to Show the Color by Setting the Viewbox left value to suit the Offset. This Approach does not let get the Color as the brush is a Visual Brush.

<Rectangle x:Name="ActiveIndexColor" Width="100" Height="15" Margin="0,180,0,0" Visibility="Visible">
    <Rectangle.Fill>
        <VisualBrush Visual="{Binding ElementName=ActiveIndex}" 
            ViewboxUnits="RelativeToBoundingBox" 
            Viewbox="0.0000001,0.0000001,0.0000001,0.0000001">
        </VisualBrush>                
    </Rectangle.Fill>            
</Rectangle>

Solution

  • Something like...

    RedColorYouWant = Offset * 0D + (1 - Offset) * 6F
    GreenColorYouWant = Offset * 00 + (1 - Offset) * FD
    BlueColorYouWant = Offset * F9 + (1 - Offset) * FD
    

    maybe ?

    Offset being a value between 0 and 1..