开发者

How to highlight a given color in C#

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


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.


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.


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.


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));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜