Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SharpDX Brush/Color inconsistency

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SharpDX Brush/Color inconsistency

    I needed to check if selected brush in Chart Style is transparent but it seems transparent Brush has different color components than transparent Color, ie:
    Color.Transparent: A=0,R=0,G=0,B=0
    solidColorBrush.Color.Transparent: A=0,R=1,G=1,B=1;

    to check it paste the following into copy of the @CandleStyle below line 69 (where candle brush is defined):
    Code:
    SolidColorBrush solidColorBrush = (SolidColorBrush) brush;
    Print("Transparent Brush: "+solidColorBrush.Color.ToString());
    Print("Transparent Color: "+Color.Transparent.ToString());
    if (solidColorBrush.Color == Color.Transparent) Print("Color transparent comparison correct");
    if (solidColorBrush.Color.Alpha == 0) Print("Color alpha comparison correct");
    and select Transparent for candle color on the chart.
    As a workaround I use alpha comparison but it looks like a bug in SharpDX implementation you should look into.

    #2
    Originally posted by gregid View Post
    I needed to check if selected brush in Chart Style is transparent but it seems transparent Brush has different color components than transparent Color, ie:
    Color.Transparent: A=0,R=0,G=0,B=0
    solidColorBrush.Color.Transparent: A=0,R=1,G=1,B=1;

    to check it paste the following into copy of the @CandleStyle below line 69 (where candle brush is defined):
    Code:
    SolidColorBrush solidColorBrush = (SolidColorBrush) brush;
    Print("Transparent Brush: "+solidColorBrush.Color.ToString());
    Print("Transparent Color: "+Color.Transparent.ToString());
    if (solidColorBrush.Color == Color.Transparent) Print("Color transparent comparison correct");
    if (solidColorBrush.Color.Alpha == 0) Print("Color alpha comparison correct");
    and select Transparent for candle color on the chart.
    As a workaround I use alpha comparison but it looks like a bug in SharpDX implementation you should look into.
    Your declaration did not assign a color. I think the default color assigned is probably Color.Empty, not Color.Transparent ?

    Comment


      #3
      Originally posted by koganam View Post
      Your declaration did not assign a color. I think the default color assigned is probably Color.Empty, not Color.Transparent ?
      I left the assigning of the color to be from property grid (Chart Style).
      Besides I didn't find Color.Empty in neither System.Windows.Media nor SharpDX.Color - it looks Color.Empty was only System.Drawing implementation.

      Comment


        #4
        gregid,

        We have an extension method in place which you can utilize:

        public static bool IsTransparent(this System.Windows.Media.Brush brush)
        ArtSenior Software Developer

        Comment


          #5
          Originally posted by NinjaTrader_Art View Post
          gregid,

          We have an extension method in place which you can utilize:

          public static bool IsTransparent(this System.Windows.Media.Brush brush)
          Thanks Art, a very helpful extension indeed.
          If possible please make equivalent extensions for SharpDX.Direct2D1.Brush.

          As to the original mismatch mentioned in my first post - if you can confirm the same behaviour I think it may be a good idea to raise it with SharpDX developers

          Comment


            #6
            Generally I am asking for more helpers in the area of Colors/Brushes

            Here is one example where getting SharpDX.Color from System.Windows.Media.Brush is troublesome:
            Code:
            SharpDX.Color neutralColor = (SharpDX.Color)((SharpDX.Direct2D1.SolidColorBrush)NeutralBrush.ToDxBrush(RenderTarget)).Color;
            There is a helper method ToDxBrush but you need to cast it first to SolidColorBrush to get Color and then the result is actually SharpDX.Color4 which needs to be casted again to SharpDX.Color - haven't confirmed it will even work as expected as am having still a lot of other conversion issues but that's the shortest way I could find so far. The alternative is getting color ARGB components and getting a new one from there.

            So what I would like to see in this case is a ToDXColor helper method and if possible make the use of Color/3/4 uniform preferably just SharpDX.Color

            Thanks

            Comment


              #7
              Hello,

              I just wanted to let you know I have put in a feature request for these items.

              If I have any updates on this I will post them as I get them.

              As always, thank you for providing the examples on this to show exactly what you mean.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                We should probably start over as I'm not sure exactly what your trying to do here and there could be a better way.

                What exactly are you doing with the SharDX brush/color?

                We only use brushes and not colors, which is why I ask. Can you walk me through the simple requirements. Heres the typical workflow:

                * You have a WPF Brush defined by the user
                * You want to reuse a WPF brush with sharpDX where we have helper method .ToDxBrush().
                * You use SharpDX brush as input to Draw on the render target.

                I would say that you should never work directly in SharpDX brushes and instead always work with WPF brushes that get converted to SharpDX automatically. This would eliminate any issues you are running into and eliminate need for more helper methods no?

                Thanks for any clarify you can provide as I was not understanding the suggestion as I ran through it.

                Comment


                  #9
                  This:

                  * You have a WPF Brush defined by the user
                  * You want to reuse a WPF brush with sharpDX where we have helper method .ToDxBrush().
                  * You use SharpDX brush as input to Draw on the render target.
                  Is exactly what I'm playing with today. Can someone provide a snippet of how to make use of ToDxBrush() to convert a user defined brush to a SharpDX brush?

                  I can't seem to crack it.

                  Comment


                    #10
                    Nevermind! I hate asking for help because the curse of asking for help is that you figure it out yourself the moment you give in to the weakness.

                    For those that come after:

                    TextBrush is my user defined WPF brush.

                    This:

                    TextBrush.ToDxBrush(RenderTarget)

                    Will do the trick. This is usable within RenderTarget.DrawTextLayout(). I think I misspelled "Render" in all previous attempts.

                    Comment


                      #11
                      Originally posted by NinjaTrader_Brett View Post
                      We should probably start over as I'm not sure exactly what your trying to do here and there could be a better way.

                      What exactly are you doing with the SharDX brush/color?

                      We only use brushes and not colors, which is why I ask. Can you walk me through the simple requirements. Heres the typical workflow:

                      * You have a WPF Brush defined by the user
                      * You want to reuse a WPF brush with sharpDX where we have helper method .ToDxBrush().
                      * You use SharpDX brush as input to Draw on the render target.

                      I would say that you should never work directly in SharpDX brushes and instead always work with WPF brushes that get converted to SharpDX automatically. This would eliminate any issues you are running into and eliminate need for more helper methods no?

                      Thanks for any clarify you can provide as I was not understanding the suggestion as I ran through it.
                      Missed your response on this one. You are assuming we are happy with standard brushes and wouldn't want create our own LinearGradientBrushes, RadialGradientBrushes, etc.
                      Try to do color interpolation (linear or cubic) on Brushes or DXBrushes and you will realize it is impossible without working on Colors.
                      Compare methods, fields, constructors, operators of Brushes:

                      vs Colors:

                      Doing anything more complex requires working on Colors.

                      The only reason I could see why you wouldn't want include the helper method is because you are worried you would have to keep explaining to users to use Brushes not Colors (just as you attempted in your post ). But this argument is not valid since I don't expect beginners to work with SharpDX objects anyway.

                      Anyway nothing crucial by any means just an idea for a nice helper to accompany ToDxBrush.

                      For those that need to get SharpDx.Color from the Brush here is the extension I use:
                      Code:
                          public static class DxBrushExtensions
                          {
                              public static SharpDX.Color ToDxColor(this SharpDX.Direct2D1.Brush dxBrush)
                              {
                                  return (SharpDX.Color)((SharpDX.Direct2D1.SolidColorBrush)dxBrush).Color;
                              }
                              public static SharpDX.Color ToDxColor(this System.Windows.Media.Brush brush, SharpDX.Direct2D1.RenderTarget renderTarget)
                              {
                                  return (SharpDX.Color)((SharpDX.Direct2D1.SolidColorBrush)brush.ToDxBrush(renderTarget)).Color;
                              }
                          }
                      Last edited by gregid; 10-31-2015, 07:57 AM.

                      Comment


                        #12
                        Brushes vs Colors can be confusing in general as you point out. It turns out you have a clear grasp on the two which is what I wanted to insure was the case.

                        A helper method I will add to the list for suggestion. Thanks for posting!

                        Comment


                          #13
                          Using the extensions in my previous post for some time I encountered some irregular errors related to sequence of the NT8 actions taken. Main culprit has been the time when RenderTarget is becoming available (not null). In some cases RenderTarget is available only after all bars are loaded which makes it impossible to access and use it outside of OnRender. It will work in 95% cases in OnBarUpdate and virtually never in OnStateChange.

                          So for those that use the above extension - to make the conversion work I changed it to the following - removing the need for render target:

                          Code:
                                  public static SharpDX.Color ToDxColor(this System.Windows.Media.Brush brush)
                                  {
                                      System.Windows.Media.Color tempColor = ((SolidColorBrush) brush).Color;
                                      return new SharpDX.Color()
                                      {
                                          A = tempColor.A,
                                          B = tempColor.B,
                                          G = tempColor.G,
                                          R = tempColor.R
                                      };
                                  }

                          Now the question to NT staff - were you considering (and planning) that some users may need/want to use RenderTarget from other methods (like OnBarUpdate)? If yes than it is a bug.

                          One example where this may be an issue is PaintBar - if you want to use custom SharpDX methods for generating paint bar brushes you would need to have it in OnBarUpdate. Additionally I would suggest adding another State to OnStateChange -> State.RenderTarget (or something similar) which would trigger once RenderTarget is available.

                          Comment


                            #14
                            Thank you gregid for your post.

                            Here's a link for anyone trying to get their head around SharpDX brushes to SharpDX colors and back again.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by rdtdale, Today, 01:02 PM
                            0 responses
                            1 view
                            0 likes
                            Last Post rdtdale
                            by rdtdale
                             
                            Started by alifarahani, Today, 09:40 AM
                            3 responses
                            15 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by RookieTrader, Today, 09:37 AM
                            4 responses
                            18 views
                            0 likes
                            Last Post RookieTrader  
                            Started by PaulMohn, Today, 12:36 PM
                            0 responses
                            6 views
                            0 likes
                            Last Post PaulMohn  
                            Started by love2code2trade, 04-17-2024, 01:45 PM
                            4 responses
                            41 views
                            0 likes
                            Last Post love2code2trade  
                            Working...
                            X