Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT8: Using Lerp with SharpDX?

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

    NT8: Using Lerp with SharpDX?

    Hi there,

    I wish to use the SharpDX method 'Lerp' with my SharpDX brush to lighten or darken a SharpDX brush.

    Edit: I have managed to work this out but I would like someone to see if this meets best practices. I also want to leave this for anyone else who needs the snippet.

    I have managed to do this:

    PHP Code:
    
     SharpDX.Direct2D1.SolidColorBrush scb =  (SharpDX.Direct2D1.SolidColorBrush)myBrushDX;                                               
     SharpDX.Color colorStart = (SharpDX.Color)scb.Color;
     SharpDX.Color colorEnd = SharpDX.Color.White;
     SharpDX.Color colorResult;
     float amount = 0.9f;
     SharpDX.Color.Lerp(ref colorStart, ref colorEnd, amount, out colorResult);
     scb = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, colorResult);
    
    myBrushDX = scb; 
    
    Thank you.
    Last edited by Sim22; 07-13-2016, 11:27 PM.

    #2
    Hi Sim22,

    I'm not sure of a best practice for lightening or darkening a color.

    If interpolation between white and the color or black and the color is working then that seems to be the solution.

    You could always add or subtract from the red green blue values themselves. The link below is for System.Windows.Media colors, but would apply to any color object that can use RGB values.
    http://www.pvladov.com/2012/09/make-...or-darker.html

    This thread will remain open to any community members that know of an alternative way of changing a color lighter or darker.
    Last edited by NinjaTrader_ChelseaB; 07-15-2016, 07:22 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you Chelsea. Yes I've seen Pladov's link before.

      I have several methods of which I am experimenting.


      PHP Code:
      
      public Color ToneChangeColor(Color color, float percentToneChange)
              {
                  if (percentToneChange < 0f)
                      return color;
      
                  byte red    = Convert.ToByte(Math.Min(255, color.R * percentToneChange * 0.01));
                  byte green    = Convert.ToByte(Math.Min(255, color.G * percentToneChange * 0.01));
                  byte blue    = Convert.ToByte(Math.Min(255, color.B * percentToneChange * 0.01));
      
                  return Color.FromArgb(color.A, red, green, blue);
              }
      
      public Brush ToneChangeBrush(Brush brush, float percentToneChange)
              {
                  if (percentToneChange < 0f)
                      return brush;
      
                  SolidColorBrush solidColorBrush = (SolidColorBrush)brush;
      
                  byte red = Convert.ToByte(Math.Min(255, solidColorBrush.Color.R * percentToneChange * 0.01));
                  byte green = Convert.ToByte(Math.Min(255, solidColorBrush.Color.G * percentToneChange * 0.01));
                  byte blue = Convert.ToByte(Math.Min(255, solidColorBrush.Color.B * percentToneChange * 0.01));
      
                  return new SolidColorBrush(Color.FromArgb(solidColorBrush.Color.A, red, green, blue));
              }
      
      public Brush ToneChangeColorToBrush(Color color, float percentToneChange)
              {
                  if (percentToneChange < 0f)
                      return new SolidColorBrush(color);
      
                  byte red = Convert.ToByte(Math.Min(255, color.R * percentToneChange * 0.01));
                  byte green = Convert.ToByte(Math.Min(255, color.G * percentToneChange * 0.01));
                  byte blue = Convert.ToByte(Math.Min(255, color.B * percentToneChange * 0.01));
      
                  return new SolidColorBrush(Color.FromArgb(color.A, red, green, blue));
              }
      
      public Color ChangeColorBrightness(Color color, float correctionFactor)
              {
                  float red = (float)color.R;
                  float green = (float)color.G;
                  float blue = (float)color.B;
      
                  if (correctionFactor < 0)
                  {
                      correctionFactor = 1 + correctionFactor;
                      red *= correctionFactor;
                      green *= correctionFactor;
                      blue *= correctionFactor;
                  }
                  else
                  {
                      red = (255 - red) * correctionFactor + red;
                      green = (255 - green) * correctionFactor + green;
                      blue = (255 - blue) * correctionFactor + blue;
                  }
      
                  return Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
              } 
      
      I was just curious with using Lerp as it had been mentioned on StackOverflow.com but I was unsure how to go about using the inbuilt Lerp method in SharpDX.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      566 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      330 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      547 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      548 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X