Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

How to GET the color of FILL RSS

6 replies

Last post Oct 23, 2007 01:42 PM by kp2712

(0)
  • TBink

    TBink

    Member

    269 Points

    235 Posts

    How to GET the color of FILL

    Sep 19, 2007 03:17 PM | LINK

    I want to GET the color of the FILL of a Rectangle....  the object xaml

    <Rectangle X:Name="Test" Fill="#FFF85B5B" />

     via script I want to get that color value and apply it to another object ... I can't find the way...

    on mouse down I tried:

    var SndFill = Sender.Fill
    var TmpRct = '<Rectangle Canvas.Left="100" Canvas.Top="100" Width="50" Height="50" Fill="'+SndFill+'" StrokeThickness="1" Stroke="blue"  />';

     This fails and when I look at the text output for TmpRct I see   Fill="SolidColorBrush"

    Sender.Fill.Color gives a non hexidecimal, long number ...  -500901 

     What's the solution...?

    tom


  • Dave Britton

    Dave Britton

    Member

    695 Points

    231 Posts

    Re: How to GET the color of FILL

    Sep 19, 2007 03:22 PM | LINK

    does fill.tostring() work?

    => Dave

    Senior Engineer/Dev Manager, Vertigo Software
    Vertigo
  • TBink

    TBink

    Member

    269 Points

    235 Posts

    Re: How to GET the color of FILL

    Sep 19, 2007 03:31 PM | LINK

    Nope...  In VisStudio I see

      Sender.Fill.toString()       "SolidColorBrush"     String

    I did do this work around since I simply want to apply the color of another object ... I can use the long value... so,

    var SndFill = Sender.Fill.Color

    var TmpRct = '<Rectangle Canvas.Left="'+(XPos-10)+'" Canvas.Top="'+(YPos-10)+'" Width="'+SWd+'" Height="'+SHt+'" Fill="red" StrokeThickness="1" Stroke="blue" Opacity="0.35" IsHitTestVisible="false"/>';

    this.TempBlock = this.control.content.CreateFromXaml(TmpRct); this.TempBlock.Fill.Color = SndFill;

    .... this works but it bothers me that I can't get a hex value for the color ....  seems to me to be a minor bug in the object model

    What I'd like to do is (bold italic):

    var TmpRct = '<Rectangle Canvas.Left="'+(XPos-10)+'" Canvas.Top="'+(YPos-10)+'" Width="'+SWd+'" Height="'+SHt+'"  Fill=="'+SndFill +'" StrokeThickness="1" Stroke="blue" Opacity="0.35" IsHitTestVisible="false"/>';

    But this fails because its a long number

    By the way there's the same issue with Stroke

    Sender.Stroke.toString()       "SolidColorBrush"     String

    tom

     

  • Jeff Paries

    Jeff Paries

    Member

    606 Points

    109 Posts

    Re: How to GET the color of FILL

    Sep 20, 2007 12:49 AM | LINK

    There is a thread on converting the color value you get here:
    http://silverlight.net/forums/t/4710.aspx 


     

  • suyog kale

    suyog kale

    Contributor

    2548 Points

    528 Posts

    Re: How to GET the color of FILL

    Oct 05, 2007 06:16 AM | LINK

     its simple

    var vobj=document.getElementById("YourSilverlightObject");
        var mainCanvas=vobj.content.findName("RectangleID");
        var mainc=Convert.ToCanvas(mainCanvas);
     
        var sc=Convert.ToSolidColorBrush(mainc.get_background());
        bgcolor=sc.get_color();
        bgcolor=getFillColor(bgcolor);

    //-------------------------------------------------------------------------------
    //-------Color value convertor function
    //-------Hexadecimal value from RGB Value
    //-------------------------------------------------------------------------------
    function getFillColor(color) {
       var fill = color;
       if (fill < 0) {
          fill = (16777216 + parseInt(fill));
       }
       fill = fill.toString(16);
       if (fill.length > 6) {
          fill = fill.substr(fill.length - 6);
       }
       while (fill.length < 6) {
          fill = "0" + fill;
       }
       fill = "#ff" + fill;
       return fill;
    }

    (If this has answered your question, please click on "Mark as Answer")

    Thanks & Rgds,
    Suyog Kale

    http://blogs.siliconindia.com/snehyog

    Senior Software Engineer
    ************************************************************
  • TBink

    TBink

    Member

    269 Points

    235 Posts

    Re: How to GET the color of FILL

    Oct 05, 2007 04:50 PM | LINK

    Many thanks... this is just what I was looking for!

  • kp2712

    kp2712

    Member

    8 Points

    7 Posts

    Re: How to GET the color of FILL

    Oct 23, 2007 01:42 PM | LINK

    Hi,I want to do something similar..I want to obtain the background of a canvas(which is a LinearGradientBrush) and assign it to a rectagle's fill property.Can you tell me how to do it?