0

How could I highlight a given color in C#. The user selects an arbitrary color for the objects I'm displaying. When I select the objects I would like to highlight them.

Do you know what is the best way to do this?

I need something like this:

private Color HighLight(Color c)
{
  //calculate a highlight color from c
  return highlighted;
}

Thanks in advance

Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
  • You're going to have to provide a lot more information. I don't understand what you're trying to accomplish, or what you mean by "calculate a highlight color". – Cody Gray - on strike Mar 01 '11 at 10:16

4 Answers4

5

The standard way to highlight an item is to use the system highlight color.

You can access that using the SystemColors.Highlight property, which returns a Color structure corresponding to the background color that the operating system uses for selected items.

The major benefit of doing it this way is that the user immediately recognizes the object as highlighted. Choosing an arbitrary color won't necessarily mean what you intend for it to mean. Being consistent with all of the other programs on their computer will go a long way towards enhancing the usability and user experience of your software.


EDIT: If you really insist on using a lightened version of the original color as the highlight color, the best way to calculate that is to convert the starting color into an alternative color space, like HSV. The RGB space was not really designed for color manipulation, and lightening a color's 3 RGB component values won't always produce the color that you expect. I provide sample code here for this conversion.

Once you've converted the color to the HSV color space, simply increase its V (or "value") attribute to lighten it, then convert it back to an RGB color. There is a built-in method to create a Color structure from RGB component values: Color.FromArgb

Note that the HSV color space is also known as HSB (where "value" is changed to "brightness"). They're perfectly equivalent in every way. That is not the same color space, however, as HSL, where L stands for "lightness". The Wikipedia article provides more information for the curious.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • This is a system text selection highlight color, that used as background on the text selection. – Viacheslav Smityukh Mar 01 '11 at 10:21
  • @Viacheslav: Yes. I called it the "system highlight color". It's intended that *all* applications use it for any highlighted regions, not just text. – Cody Gray - on strike Mar 01 '11 at 10:23
  • I strongly recommend against mixing system colors with application-defined colors; if the user has changed their system colors so that the highlight color happens to match the color of whatever it's highlighting in your application, it will become invisible to that user! – Sphynx Oct 17 '17 at 19:48
  • Yup, valid point, @Sphynx. Just didn't think to mention it in this answer. Not only should you not mix system colors with app-defined colors, you also need to choose logically consistent combinations of system colors. Relevant blog post: http://blogs.msdn.com/b/oldnewthing/archive/2007/12/12/6648399.aspx. For highlighting in particular, for monochromatic items, you should use SystemColors.Highlight as the background and SystemColors.HighlightText as the foreground to be assured sufficient contrast. With multi-colored foreground items, you can alpha-blend the highlight color with the foreground – Cody Gray - on strike Oct 18 '17 at 08:34
2

You need to convert your RGB color to HSL (follow this post). Then increase L value (lightness) and convert back to RGB. Changing R, G, B directly will not give you "natural" looking of highlighted color. But all depends of what will you do in your application. Converting gives best result but need a lot of code. On the other hand, changing RGB directly will work very fast.

Community
  • 1
  • 1
kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
  • Where is no easy way to convert HSL to the Color, isn't it? – Viacheslav Smityukh Mar 01 '11 at 10:19
  • This is another question :) Dont know exactly if standard library has easy methods for this. Converting to HSL will give best results comparing with just increasing R, G and B, because will not destruct color. – kyrylomyr Mar 01 '11 at 10:23
1

This should work for you:

private Color HighLight(Color c, int highlightfactor)
{
  //calculate a highlight color from c
  return Color.FromArgb(c.R + highlightfactor > 255 ? 255 : c.R + highlightfactor,
                        c.G + highlightfactor > 255 ? 255 : c.G + highlightfactor,
                        c.B + highlightfactor > 255 ? 255 : c.B + highlightfactor
    );
}

A higher highlightfactor gives you a lighter color in return.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • It will work perfect only with "clear" colors (red, blue, green, etc). – kyrylomyr Mar 01 '11 at 10:18
  • @archer - You are correct, but in my experience, it gives a decent result for other colors also. – Øyvind Bråthen Mar 01 '11 at 10:21
  • 2
    @archer: No, it will work with other colors, too. The problem is that the RGB color space wasn't really designed for color manipulation. Lightening a color's RGB component values won't always produce the color you expect. You need to convert to a different color space like HSV, which is much more natural for humans. See [here](http://stackoverflow.com/questions/4123998/algorithm-to-switch-between-rgb-and-hsb-color-values/4124469#4124469) for sample code that does just that. – Cody Gray - on strike Mar 01 '11 at 10:22
1

You can increment each base color

private Color HighLight(Color c)
{
  var delta = 30;
  return Color.FromArgb(Math.Max(255, c.R + delta), Math.Max(255, c.G + delta), Math.Max(255, c.B + delta));
}
Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42