Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

User friendly gradient colors

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

    User friendly gradient colors

    I use a gradient color in one of my indicators which I am happy to share on the EcoSystem.
    However I would like to make the choice of gradient colors úser friendly
    Here is my code that makes the gradient color
    Code:
            new SharpDX.Direct2D1.GradientStopCollection(RenderTarget, new SharpDX.Direct2D1.GradientStop[]
                {
                    //colorenum = Enum.Parse(typeof(KnownColor),"Black");
                    new    SharpDX.Direct2D1. GradientStop()
                    {
    
                        Color = SharpDX.Color.HotPink,/// grey horrible
                        Position = 0,
                    },
                    new SharpDX.Direct2D1. GradientStop()
                    {
    
                        Color = SharpDX.Color.AntiqueWhite,
                        Position = 1,
                    }
                }));​
    I would like to have some color choices that the user decides but I can't work out how to get these to become
    a SharpDX4 color.
    I have looked at the help guides but can only get so far as an argb which is most un user friendly.
    I just want the user to be able to pick eg ForestGreen and Yellow as the two ingredients for the GradientStop colors.

    Any help much appreciated.

    #2
    Hello Mindset,

    You will want to get the System.Windows.Media.Brush with the enum, and then use .ToDxBrush(RenderTarget) to convert this to a SharpDXBrush. I advise to do this in OnRenderTargetChanged().


    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yes I saw that but I get an implicit conversion error message

      Code:
              protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
              {
                   base.OnRender(chartControl, chartScale);
                  SharpDX.Direct2D1.Brush dxBrush                         = TextBrush.ToDxBrush(RenderTarget);
      
                  NinjaTrader.Gui.Tools.SimpleFont simpleFont                 = TextFont;
                  SharpDX.DirectWrite.TextFormat textFormat1                     = simpleFont.ToDirectWriteTextFormat();
                  SharpDX.DirectWrite.TextLayout textLayout1                     = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory, timeLeft, textFormat1, ChartPanel.X + ChartPanel.W, textFormat1.FontSize);
                  SharpDX.Vector2 upperTextPoint     = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W/2-(float)(simpleFont.Size *1.5), ChartPanel.Y + ChartPanel.H/2 -(float)(simpleFont.Size * 1.5));            
      
                      using (dxBrush)
      {
      ​            RenderTarget.DrawTextLayout(upperTextPoint, textLayout1, dxBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
      
                  SharpDX.Vector2 startPoint                                     = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
                  SharpDX.Vector2 endPoint                                     = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H);
                  SharpDX.Direct2D1.LinearGradientBrush linearGradientBrush     = new SharpDX.Direct2D1.LinearGradientBrush(RenderTarget, new SharpDX.Direct2D1.LinearGradientBrushProperties()
                  {
                      StartPoint = new SharpDX.Vector2(0, startPoint.Y),
                      EndPoint = new SharpDX.Vector2(0, endPoint.Y),
      
                  },
      
                  new SharpDX.Direct2D1.GradientStopCollection(RenderTarget, new SharpDX.Direct2D1.GradientStop[]
                  {
      
                      new    SharpDX.Direct2D1. GradientStop()
                      {
      
      [B] [COLOR=#e74c3c]   Color = dxBrush,//error[/COLOR][/B]
                          Position = 0,
                      },
                      new SharpDX.Direct2D1. GradientStop()
                      {
      
                          Color = SharpDX.Color.AntiqueWhite,
                          Position = 1,
                      }
                  }));
      ​}
      Last edited by Mindset; 12-02-2022, 01:15 AM. Reason: code amendment

      Comment


        #4
        Hello Mindset,

        What is the error message?

        What line of code is causing the error?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          So the error is this
          Cannot implicity convert type SharpDX.Direct2D1.Brush to SharpDX.Color4
          Which is one of the gradient colors in the above code

          Comment


            #6
            Hello Mindset,

            To confirm, the first line is causing the error?

            SharpDX.Direct2D1.Brush dxBrush = TextBrush.ToDxBrush(RenderTarget);

            If you comment out all other code and leave this line, the error persists?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea
              No, that line is fine. But I am trying to get this Brush color into a linear gradient color. So as I understand it a SharpDX.Color4 is a construct whereas I am putting in a brush which i guess it doesn't like!
              I can't beleive how difficult this is to explain - the two lines that say Color below are where I want to give the user the option to create their own
              gradientStop eg black & white, Yellow& green, and so on.
              So I need to create a user option to designate the SharpDx.Color.Black - make sense?


              Code:
              new SharpDX.Direct2D1.GradientStopCollection(RenderTarget, new SharpDX.Direct2D1.GradientStop[]
                          {
              
                              new    SharpDX.Direct2D1. GradientStop()
                              {
              
                                  Color =  SharpDX.Color.Black,
                                  Position = 0,
                              },
                              new SharpDX.Direct2D1. GradientStop()
                              {
              
                                  Color =  SharpDX.Color.AntiqueWhite,
                                  Position = 1,
                              }
                          }));​

              Comment


                #8
                Hello Mindset,

                It helps to provide the specific code causing the error, as requested.

                To confirm, the issue is with:
                new SharpDX.Direct2D1.GradientStop() { Color = SharpDX.Color.Black, Position = 0 }

                ​Is this correct?

                I am not able to reproduce. After correcting the incorrect syntax, I am finding this compiles without issue.

                DxBrushGradientTest_NT8.zip

                I also tested that entire code block and after correcting the syntax, this also compiled without issue.


                Where you have stated:
                "So I need to create a user option to designate the SharpDx.Color.Black - make sense?"

                You would need to do this with a System.Media.Brush, and then use .ToDxBrush() in OnRender to convert this to a DxBrush.


                See post # 2.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chelsea
                  The OnRender code was produced by Patrick_H when he translated one of my original NT7 indicators here.

                  Your code does indeed compile but I can't translate what you have done to my code - it simply does not compile.
                  For brevity I produce my ONRender code and OnRenderTargetChanged below so maybe you can see the issue.
                  If I plug your changes in it fails at the word

                  SharpDX.Direct2D1.GradientStop myStop = new SharpDX.Direct2D1.GradientStop() { Color = SharpDX.Color.Yellow, Position = 0 };

                  SharpDX.Direct2D1.GradientStopCollection myGSC = new SharpDX.Direct2D1.GradientStopCollection(RenderTar get, new SharpDX.Direct2D1.GradientStop[]


                  Code:
                          #region OnRender
                          protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                          {
                               base.OnRender(chartControl, chartScale);
                  
                              NinjaTrader.Gui.Tools.SimpleFont simpleFont                 = TextFont;
                              SharpDX.DirectWrite.TextFormat textFormat1                     = simpleFont.ToDirectWriteTextFormat();
                              SharpDX.DirectWrite.TextLayout textLayout1                     = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory, timeLeft, textFormat1, ChartPanel.X + ChartPanel.W, textFormat1.FontSize);
                              SharpDX.Vector2 upperTextPoint     = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W/2-(float)(simpleFont.Size *1.5), ChartPanel.Y + ChartPanel.H/2 -(float)(simpleFont.Size * 1.5));            
                  
                              RenderTarget.DrawTextLayout(upperTextPoint, textLayout1, dxBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                  
                              SharpDX.Vector2 startPoint                                     = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
                              SharpDX.Vector2 endPoint                                     = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H);
                              SharpDX.Direct2D1.LinearGradientBrush linearGradientBrush     = new SharpDX.Direct2D1.LinearGradientBrush(RenderTarget, new SharpDX.Direct2D1.LinearGradientBrushProperties()
                              {
                                  StartPoint = new SharpDX.Vector2(0, startPoint.Y),
                                  EndPoint = new SharpDX.Vector2(0, endPoint.Y),
                  
                              },
                              new SharpDX.Direct2D1.GradientStopCollection(RenderTarget, new SharpDX.Direct2D1.GradientStop[]
                              {
                                  new    SharpDX.Direct2D1. GradientStop()
                                  {
                                      /// blue/cyan is nice, green yellow is sunset
                                      Color =  SharpDX.Color.Black,
                                      Position = 0,
                                  },
                                  new SharpDX.Direct2D1. GradientStop()
                                  {
                                      Color =  SharpDX.Color.AntiqueWhite,
                                      Position = 1,
                                  }
                              }));
                  
                              if(fType != FlashType.None && (int)barTimeLeft.TotalSeconds < var2  && (int)barTimeLeft.TotalSeconds > var1)
                              {
                                  if((int)barTimeLeft.TotalSeconds % 2 < 1 )///alternate colors every other second
                                  {                    
                                      if(fType == FlashType.WholePanel)
                                      {                        
                                          SharpDX.RectangleF rect = new SharpDX.RectangleF(startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
                                          RenderTarget.FillRectangle(rect, linearGradientBrush);
                                      }
                                      else if(fType == FlashType.ClockFaceOnly)
                                      {                        
                                          SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(ChartPanel.X + 5, ChartPanel.Y + (ChartPanel.H - textLayout1.Metrics.Height));
                                          SharpDX.RectangleF rect = new SharpDX.RectangleF(upperTextPoint.X, upperTextPoint.Y, textLayout1.Metrics.Width, textLayout1.Metrics.Height);
                  
                                          RenderTarget.FillRectangle(rect, linearGradientBrush);
                                          RenderTarget.DrawTextLayout(upperTextPoint, textLayout1, dxBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                                      }
                                      else if(fType == FlashType.TextOnly)
                                      {
                                          RenderTarget.DrawTextLayout(upperTextPoint, textLayout1, linearGradientBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
                                      }
                                  }
                              }
                  
                              linearGradientBrush.Dispose();
                              dxBrush.Dispose();
                              textFormat1.Dispose();
                              textLayout1.Dispose();
                          }
                          public override void OnRenderTargetChanged()
                  {
                    // if dxBrush exists on first render target change, dispose of it
                    if (dxBrush != null)
                    {
                      dxBrush.Dispose();
                    }
                  
                    // recalculate dxBrush from user defined brush when RenderTarget is recreated
                    if (RenderTarget != null)
                      dxBrush = UserBrush1.ToDxBrush(RenderTarget);
                  }​
                  
                          #endregion
                  ​

                  Comment


                    #10
                    Mindset It sounds like you want a way to specify (at least) two specific colours for SharpDX, correct? Presumably within the Indicator parameters window?

                    If so, you could simply use a standard WPF Brush parameter which gives easily selected brush colours, and then within your rendering convert that to a SharpDX Brush.

                    You may already have looked here, but the NT8 Help on Using SharpDX for Custom Chart Rendering is helpful. Basically, take the WPF Brush and create your new ShaprDX Brush with the ToDxBrush() method. Would that do what you're trying to achieve?

                    You can get standard Brushes in your Indicator as follows:
                    Code:
                            [XmlIgnore]
                            [Display(GroupName = "Gradient Colours", Order = 1, Name = "First Colour", Description = "First Colour for Gradient")]
                            public  Brush  FirstColour { get { return  _FirstColour; }
                                                         set { _FirstColour = value; } }
                    
                            private Brush _FirstColour = Brushes.Yellow;
                    
                            [Browsable(false)]
                            public string FirstColourSerialized
                            {
                                get { return                  Serialize.BrushToString(FirstColourSerialized); }
                                set { FirstColourSerialized = Serialize.StringToBrush(value); }
                            }​
                    Thanks.​
                    Multi-Dimensional Managed Trading
                    jeronymite
                    NinjaTrader Ecosystem Vendor - Mizpah Software

                    Comment


                      #11
                      Hello Jeronymite
                      Yes you understand perfectly!
                      I kind of got where you are but the issue is that I don't want a brush I want a colour which under SharpDx linear gradient appears to be different.
                      I can get the Brush colour from WPF and I can convert it by using ToDXBrush but if I try to put that in as a color in GradientStop it gives me an implicit conversion error message.
                      Very frustrating as I just can't work it out.
                      I know I am just missing a small conceptual jump - it's just got me completely foxed at the moment

                      Code:
                      SharpDX.Direct2D1.Brush dxBrush = FirstColour.ToDxBrush(RenderTarget);
                      
                                      using (dxBrush)
                                      {
                      
                                  SharpDX.Direct2D1.LinearGradientBrush linearGradientBrush     = new SharpDX.Direct2D1.LinearGradientBrush(RenderTarget, new SharpDX.Direct2D1.LinearGradientBrushProperties()
                                  {
                                      StartPoint = new SharpDX.Vector2(0, startPoint.Y),
                                      EndPoint = new SharpDX.Vector2(0, endPoint.Y),
                      
                                  },
                      
                                  new SharpDX.Direct2D1.GradientStopCollection(RenderTarget, new SharpDX.Direct2D1.GradientStop[]
                                  {
                      
                                      new    SharpDX.Direct2D1. GradientStop()
                                      {
                      
                                          Color =  dxBrush,//SharpDX.Color.Black,///[B]FAILS HERE[/B]
                                          Position = 0,
                                      },
                                      new SharpDX.Direct2D1. GradientStop()
                                      {
                      
                                          Color =  SharpDX.Color.AntiqueWhite,
                                          Position = 1,
                                      }
                                  }));​

                      Comment


                        #12
                        Hello Mindset,

                        Starting with the export I provided, which is able to compile without issue, what are you adding to this that is causing it to no longer compile?

                        Is the using necessary? Can you remove that?

                        I also recommend converting the brushes in OnRenderTargetChanged().

                        Below is a link to an example.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Chelsea_B
                          I am a bit lost here.
                          I basically cannot convert a Brush to a linear gradient color - all the examples you have kindly provided translate a media brush to a dxBrush but that does not work
                          with linear gradient colors. Or at least I can't get it to work.
                          If I write text and use the toDxBrush converter then the "translation" of the brushes works just as you would expect.
                          It's the linear gradient color that is the issue.

                          Comment


                            #14
                            Mindset Subject to alternatives from NinjaTrader_ChelseaB you could try creating a WPF SolidColorBrush instead of a WPF Brush. Then maybe you could use something like this:
                            Code:
                            SolidColorBrush scb = Brushes.Salmon;
                            SharpDX.Color sharpColor = new SharpDX.Color(scb.Color.R,scb.Color.G,scb.Color.B);​
                            I have not done this in working code, but it compiles and would seem to be reasonable. Not exactly the most elegant but not unreasonable either. Just a thought.

                            Thanks.
                            Multi-Dimensional Managed Trading
                            jeronymite
                            NinjaTrader Ecosystem Vendor - Mizpah Software

                            Comment


                              #15
                              Aha - now we are getting somewhere. Many many thanks jeronymite. Much appreciated.
                              I gather it needs freezing, etc so I will work all that out and when I have finalised the code and made sure there are no glitches I will post it here.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, 04-16-2024, 06:09 PM
                              4 responses
                              12 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by terofs, Today, 04:18 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post terofs
                              by terofs
                               
                              Started by nandhumca, Today, 03:41 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post nandhumca  
                              Started by The_Sec, Today, 03:37 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post The_Sec
                              by The_Sec
                               
                              Started by GwFutures1988, Today, 02:48 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Working...
                              X