C#:APP: Tint Color

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShadowLocke
    New Member
    • Jan 2008
    • 116

    C#:APP: Tint Color

    How can I tint one color with another color? i.e. say i have a Color.LightSkyB lue
    (135,206,250) i want to tint it with Color.Green(0,1 28, 0)

    Whats the math behind it?
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Originally posted by ShadowLocke
    How can I tint one color with another color? i.e. say i have a Color.LightSkyB lue
    (135,206,250) i want to tint it with Color.Green(0,1 28, 0)

    Whats the math behind it?
    Couldn't you just use some application (like Paint.NET) that has a color wheel or mixer, find the color you actually want and then use that RRGGBB value?

    Why do you want the program to blend your colors?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Well I had really though I remember seeing something in the Color class about tinting but I can't find it anymore.
      Have you read through this? http://www.inform.umd.edu/MCTP/Cours...son/index.html

      Comment

      • ShadowLocke
        New Member
        • Jan 2008
        • 116

        #4
        Originally posted by Plater
        Well I had really though I remember seeing something in the Color class about tinting but I can't find it anymore.
        Have you read through this? http://www.inform.umd.edu/MCTP/Cours...son/index.html
        I was thinking the same. I have found a class that may do it for me.

        System.Drawing. Imaging.ColorMa trix

        I havent had a chane to tinker with it too much, but it looks like this is gonna work for me. (great reference btw.)

        Comment

        • ShadowLocke
          New Member
          • Jan 2008
          • 116

          #5
          This will do, heres what I came up with:

          Code:
                  private Bitmap TintBitmap(ref Bitmap b, Color tintColor)
                  {
                      ImageAttributes ia = new ImageAttributes();
                      float tR = (float)tintColor.R / 255;
                      float tG = (float)tintColor.G / 255;
                      float tB = (float)tintColor.B / 255;
          
                      ColorMatrix cm = new ColorMatrix(new float[][]
                      {
                          new float[] {1, 0, 0, 0, 0},
                          new float[] {0, 1, 0, 0, 0},
                          new float[] {0, 0, 1, 0, 0},
                          new float[] {0, 0, 0, 1, 0},
                          new float[] {tR, tG, tB, 0, 1}
                      });
                      
                      ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
          
                      Bitmap result = new Bitmap(b.Width, b.Height);
                      Point[] pts = new Point[] { new Point(0, 0), new Point(b.Width, 0), new Point(0, b.Height) };
                      Rectangle rect = new Rectangle(0, 0, b.Width, b.Height);
          
                      using (Graphics g = Graphics.FromImage((Image)result))
                      {
                          g.DrawImage(b, pts, rect, GraphicsUnit.Pixel, ia);
                      }
          
                      return result;
                  }

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Do you need to pass bitmap b in by reference? (Is that just to keep memory usage down for very large bitmap objects?)

            Comment

            • ShadowLocke
              New Member
              • Jan 2008
              • 116

              #7
              Originally posted by Plater
              Do you need to pass bitmap b in by reference? (Is that just to keep memory usage down for very large bitmap objects?)
              Correct, one of my many feeble attempts at minimizing memory consumption.

              Comment

              Working...