Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Inflating color value

Inflating color value

Scheduled Pinned Locked Moved WPF
databasehelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Prasoon Chaudhary
    wrote on last edited by
    #1

    I have a very basic query. Let's say we have a Color1, and I need Color2 to be

    Color.FromRgb (Math.Min(Color1.R+10,255), Math.Min(Color1.G+10,255), Math.Min(Color1.B+10,255));

    But I am not able to do this simple thing. Basically, I've got stuck with integer and byte conversion. Please suggest in following code-

    private Color GetNewColor(Color colorParameter)
    {
    Color newColor;
    //Set value of newColor here
    return newColor;
    }

    //Color1 = Color.FromRgb(255, 0, 0);
    //return GetNewColor(Color1);
    //It should return (255,10,10)

    T C 2 Replies Last reply
    0
    • P Prasoon Chaudhary

      I have a very basic query. Let's say we have a Color1, and I need Color2 to be

      Color.FromRgb (Math.Min(Color1.R+10,255), Math.Min(Color1.G+10,255), Math.Min(Color1.B+10,255));

      But I am not able to do this simple thing. Basically, I've got stuck with integer and byte conversion. Please suggest in following code-

      private Color GetNewColor(Color colorParameter)
      {
      Color newColor;
      //Set value of newColor here
      return newColor;
      }

      //Color1 = Color.FromRgb(255, 0, 0);
      //return GetNewColor(Color1);
      //It should return (255,10,10)

      T Offline
      T Offline
      Tony Richards
      wrote on last edited by
      #2

      What exactly is going wrong? Are you getting an exception or a compiler error or is it just not doing what you expect? One possible issue I can see is an overflow of the colour component. For example, with a byte, if you add 10 to 255 you'll actually end up with 9, and Math.Min won't work as you expect. You can catch this problem by surrounding the code with a checked statement, which will cause the runtime to throw on an overflow. The quickest solution to this would be to cast the byte to an int first, do the addition, and cast the result back to a byte (once you're certain it'll fit).

      P 1 Reply Last reply
      0
      • T Tony Richards

        What exactly is going wrong? Are you getting an exception or a compiler error or is it just not doing what you expect? One possible issue I can see is an overflow of the colour component. For example, with a byte, if you add 10 to 255 you'll actually end up with 9, and Math.Min won't work as you expect. You can catch this problem by surrounding the code with a checked statement, which will cause the runtime to throw on an overflow. The quickest solution to this would be to cast the byte to an int first, do the addition, and cast the result back to a byte (once you're certain it'll fit).

        P Offline
        P Offline
        Prasoon Chaudhary
        wrote on last edited by
        #3

        Thanks for the reply! Problem was with casting only. I've sorted that out now. Trivial, but just thought of sharing it:

        byte r = Convert.ToByte(Math.Min(255, Color1.R + 10));
        byte g = Convert.ToByte(Math.Min(255, Color1.G + 10));
        byte b = Convert.ToByte(Math.Min(255, Color1.B + 10));
        return Color.FromRgb(r, g, b);

        1 Reply Last reply
        0
        • P Prasoon Chaudhary

          I have a very basic query. Let's say we have a Color1, and I need Color2 to be

          Color.FromRgb (Math.Min(Color1.R+10,255), Math.Min(Color1.G+10,255), Math.Min(Color1.B+10,255));

          But I am not able to do this simple thing. Basically, I've got stuck with integer and byte conversion. Please suggest in following code-

          private Color GetNewColor(Color colorParameter)
          {
          Color newColor;
          //Set value of newColor here
          return newColor;
          }

          //Color1 = Color.FromRgb(255, 0, 0);
          //return GetNewColor(Color1);
          //It should return (255,10,10)

          C Offline
          C Offline
          Christian Amado
          wrote on last edited by
          #4

          In Silverlight you can do this:

          private Color GetNewColor(Color colorParameter)
          {
          Color newColor;
          Color newcolor = Color.FromArgb(0, 255, 10, 10);
          return newColor;
          }

          You can read this[^] too. Regards

          Christian Amado MCITP | MCTS | MOS | MTA Olimpia ☆ ★★★ Please mark as answer, if it helps.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups