开发者

How to make the Color class helper?

开发者 https://www.devze.com 2022-12-14 00:59 出处:网络
I am trying to make a class helper for the Color class in C#. I am a Delphi programmer and as far as I know, class helpers allow you to expand base classes so when you create an instance of the base c

I am trying to make a class helper for the Color class in C#. I am a Delphi programmer and as far as I know, class helpers allow you to expand base classes so when you create an instance of the base class, you have access not only to the base methods, but also to all those defined in the helper class. Is it possible to achieve a similar effect in C#? Let's say, I have the following static method:

public static Color AdjustForeColor(Color backColor)
{
  double mediumColor = ((0.3 * 255.0) + (0.59 * 255.0) + (0.11 * 255.0)) / 2.0;
  if ((0开发者_开发问答.3 * backColor.R) + (0.59 * backColor.G) + (0.11 * backColor.B) > mediumColor)
    return Color.Black;
  else
    return Color.White;
}

It adjusts the font colour to the background so that it stays readable. I would like this method to be accessible through the Color class (Color.AdjustForeColor()). How to do that?

Thanks in advance.

Mariusz.


It is called extension methods in C#

0

精彩评论

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