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

NT7 to 8 conversion - getting Plot color

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

    NT7 to 8 conversion - getting Plot color

    Can you please help me get to the equivalent code in NT8:

    In NT7:

    string myPlotColor = Plots[1].Pen.Color.ToString();
    Print (myPlotColor);

    would print to output window something like:

    Color[Green]

    In NT8, I keep getting output in hex (which I don't want) when I try variations of "Plots[1].Pen.Brush.ToString()" to get the plot color set in the UI.

    Thank-you.

    #2
    Hello REI140205,

    Thank you for your post.

    In NT8 all "Color" objects are now "Brush" objects, so the syntax for this is very similar:

    Code:
    Plots[0].Pen.Brush
    https://ninjatrader.com/support/help...th_brushes.htm

    The Hex code output would be correct for this type of object, what specifically are you trying to do with the string representation of the color?

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

    Comment


      #3
      The colors are used as words in a text file (e.g., Red, Green, Magenta). Hex format is not user friendly..

      Comment


        #4
        Hello REI140205,

        Thank you for the additional details.

        For this use case, this is likely something you will need to create with custom logic to read the hex code and compare that to an existing list of color names / associated hex codes. NT8 can now have custom colors in the brush selector so using the name like this may not always apply or not all brushes used will actually have a name. You can create a method to compare hex codes to see if the one being used matches a known name, however this would require the use of a C# conecpt called reflection and is not something NinjaScript specifically offers as a supported/documented concept.

        Previously in NT7 because it was using the System.Drawing.Color objects there were different stipulations surrounding how this object worked. One of those would be how its ToString() method outputted a color name instead of RGB or HEX.

        For this question, you may need to do further research to find C# workarounds to achieve gathering the name from a System.Windows.Media.Brush object. Here is one example of iterating over the Brushes object to collect name and hex code, this uses Reflection which is a C# topic. You can review more information about using Reflection by searching online, however, this is not a topic our support could further detail.

        Code:
        Type t = typeof(Brushes);
        System.Reflection.PropertyInfo[] props = t.GetProperties();
        IEnumerable<System.Reflection.PropertyInfo> brs = from p in props where typeof(Brush).IsAssignableFrom(p.PropertyType) select p;
        if (brs.Count() > 0)
        {
            foreach (System.Reflection.PropertyInfo info in brs)
            {
                Print(info.Name + " " + info.GetValue(t).ToString());
            }
        }


        I look forward to being of further assistance.


        JesseNinjaTrader Customer Service

        Comment


          #5
          Ok, I understand - no simple fix. Thanks. So if I get the color in hex (e.g., "#FF008000"), what is the code snippet to covert the hex back to a brush with color green (in this case)? Do I have to break the hex into bytes and then do this from the "Working with Brushes" help topic:

          // initiate new solid color brush which has an alpha (transparency) value of 100
          MyBrush = new SolidColorBrush(Color.FromArgb(100, 56, 120, 153)); //so here my #FF008000 becomes (FF,00,80,00)
          myBrush.Freeze();

          Comment


            #6
            Hello REI140205,

            Thank you for your reply.

            You shouldn't need to do that, and for this type of question, because this is just a C# concept you will generally have good luck doing a google search for this specifically. A sample search term would be "system.windows.media.brush from hex". Using the fully qualified name like this in the search will bring up the most relevant C# answers without specific C# terms in the search.

            StackOverflow will often be the first result with a question/answer, here is an answer which does not require converting to bytes: https://stackoverflow.com/questions/...to-media-brush

            Code:
            var converter = new System.Windows.Media.BrushConverter();
            var brush = (Brush)converter.ConvertFromString("#FFFFFF90");
            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks again. None of the Stack Overflow examples mention the need to include the code: myBrush.Freeze(). Is it required?

              Comment


                #8
                Hello REI140205,

                Freezing a brush would be required if you create a brand new brush and plan to modify it later such as changing its opacity. In this case, because a new brush is being generated from a string I would recommend doing so if the brush is not frozen. I don't believe it would be required unless you are making a dynamic brush which changes however you can also check if a brush is or is not frozen:

                Code:
                Brush b = Brushes.Red;
                if(!b.IsFrozen) b.Freeze();
                You won't see any errors calling .Freeze() on a brush that is already frozen so I don't believe it would make much of a difference here if you did call Freeze for each brush. The one important note I can relay here is any brush which is used by NinjaTrader for rendering must be frozen, so it is a good habit to check your custom brushes to ensure you freeze them.


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

                Comment


                  #9
                  This is the closest topic I could find to converting a brush color to rgb.

                  I want to input the color and the transparency for a plot. I have no problem inputing the transparency as well as the color and serializing it.

                  This is what I am attempting to do.
                  Plots[i].Brush = new SolidColorBrush(Color.FromArgb(styleAtransparency, rVal, gVal, bVal));

                  where for rVal, gVal and bVal I need to get from the brush color. I have found this that uses an extension method to create a ToArgb as the inverse of FromArgb.

                  https://stackoverflow.com/questions/...menting-toargb

                  I have tried the Opacity = but have not been able to get it to work.
                  Plots[i].Brush = new SolidColorBrush(Colors.Blue) {Opacity = 0.25}; // need Colors.Blue to be the color of the Brush that I input.

                  Is there an easier way or should I attempt to use the extension?

                  Any help greatly appreciated.

                  Comment


                    #10
                    I found a simple way to get the rgb values from the Serialized version of the color.

                    rVal = Convert.ToByte(StyleAColorSerializable.Substring(8 6,2),16);
                    gVal = Convert.ToByte(StyleAColorSerializable.Substring(8 8,2),16);
                    bVal = Convert.ToByte(StyleAColorSerializable.Substring(9 0,2),16);
                    Then I just created a brush with the alpha transparancy
                    Plots[i].Brush = new SolidColorBrush(Color.FromArgb(styleAtransparency, rVal, gVal, bVal));

                    Maybe not the best; other ideas appreciated.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by junkone, Today, 11:37 AM
                    2 responses
                    12 views
                    0 likes
                    Last Post junkone
                    by junkone
                     
                    Started by frankthearm, Yesterday, 09:08 AM
                    12 responses
                    43 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by quantismo, 04-17-2024, 05:13 PM
                    5 responses
                    35 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by proptrade13, Today, 11:06 AM
                    1 response
                    7 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by love2code2trade, 04-17-2024, 01:45 PM
                    4 responses
                    35 views
                    0 likes
                    Last Post love2code2trade  
                    Working...
                    X