• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Trade95, Today, 05:38 PM
                1 response
                4 views
                0 likes
                Last Post NinjaTrader_EricB  
                Started by Karelgp7, Today, 03:16 PM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by orenytz, Today, 01:18 PM
                1 response
                20 views
                0 likes
                Last Post NinjaTrader_PaulH  
                Started by des.kkeng, Today, 01:10 PM
                1 response
                11 views
                0 likes
                Last Post NinjaTrader_PatrickG  
                Started by Capablanca, Today, 12:35 PM
                1 response
                10 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X