Вопрос

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>
Это было полезно?

Решение

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..

Другие советы

Thanks franssu for the beautiful idea!

To suit my offset I wrote my code as follows:

int red = ((offset * 13) + ((100 - offset) * 111)) / 100;
int green = ((offset * 0) + ((100 - offset) * 253)) / 100;
int blue = ((offset * 249) + ((100 - offset) * 253)) / 100;
Color color = (Color)ColorConverter.ConvertFromString("#" + red.ToString("x2") +  green.ToString("x2") + blue.ToString("x2"));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top