1

I am trying add variety to Color class for example:

Color c = Color.clearWhite;  //Should return RGBA (1, 1, 1, 0).

or

Color y = Color.trueYellow;  //Should return RGBA(1,1,0,1).

etc.

is there a way to implement this? thanks in advance.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Do you mean add it to `System.Drawing.Color` ? If so you can't, that is not code you can modify. – Igor Dec 17 '20 at 14:04
  • 2
    No. You can create your own `MyColor` class, with its own static properties of your choosing, however. – canton7 Dec 17 '20 at 14:04
  • Closest you can get to this kind of syntax ATM is to create extension methods on the `System.Drawing.Color` and then invoke it like this: `default(Color).TrueYellow()` – Dusan Dec 17 '20 at 14:09
  • Does this answer your question? [Static extension methods](https://stackoverflow.com/questions/866921/static-extension-methods) – Thomas Dec 17 '20 at 14:57
  • @Thomas: Color is a struct, not a static class. It is possible the way Dusan suggests. – Palle Due Dec 17 '20 at 15:07
  • @Palle Due: This is still not an injection of a static property, but creation of an instance and calling a method on it. – Thomas Dec 17 '20 at 18:42
  • @Igor I'm pretty sure OP is referring to [`UnityEngine.Color`](https://docs.unity3d.com/ScriptReference/Color.html) hence the tag for `unity3d` ;) – derHugo Dec 17 '20 at 22:48
  • @Dusan same as above comment ;) .. to create a default instance and then fill in different values for the fields in the extension method sounds a valid solution. Questionable though why not simply instantiate it with the correct values right away or create a static readonly field in some other static class ^^ – derHugo Dec 17 '20 at 22:52
  • @Thomas: Sorry, didn't follow the link.I read the title as being about static classes. My bad. – Palle Due Dec 18 '20 at 07:23

0 Answers0