Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color hue in XNA?

Tags:

c#

colors

xna

argb

I have a color of class Microsoft.Xna.Framework.Color. How can I change its hue (or get a new color with slightly different hue). Should I convert it to System.Drawing.Color, then somehow change it and convert back? I can't find any useful information on this anywhere.

EDIT Example: I have red color R:255, G:0, B:0. Now I want to get slightly more orange color. Then if I get this color and transform it again I'll get even more orange color, then after some transformations I'll go to yellow, green etc. I don't know exact values of ARGB of each color and I don't need them. I just need to change hue of a color by some factor (for example 10 degrees).

like image 861
Episodex Avatar asked Feb 19 '23 21:02

Episodex


2 Answers

You should use the A R G B properties and change the values to get different nyansers.

For example:

Color color = new Color(0,0,0);
//Then you can change the argb properties:
color.A = 10;
color.R = 15;
color.G = 9;
color.B = 25;

If I understand you need something like this:

public static class Utilities
{
    public static void Increase(this Color color, int value)
    {
        if(color.R >= color.G && color.R >= color.B)
           color.R += value;
        else if(color.G >= color.R && color.G >= color.B)
           color.G += value;
        else
           color.B += value;
    }

    public static void Decrease(this Color color, int value)
    {
        if(color.R <= color.G && color.R <= color.B)
           color.R -= value;
        else if(color.G <= color.R && color.G <= color.B)
           color.G -= value;
        else
           color.B -= value;
    }
}

Then:

Color myColor = new Color(10,0,0);
myColor.Increase(10);
//or
myColor.Decrease(10);
like image 83
Omar Avatar answered Feb 22 '23 11:02

Omar


according to this documentation, you can pass whatever RGB(A) values you want into the XNA color class constructor. You can also use the R, B, and G properties to change them afterwards

example:

Color myColor = new Color(150, 100, 100);

myColor.R = 200

that example will change a red to a slightly deeper red.

An example of making a color go from Red to orange to yellow to green would be

Color myColor = new Color(255, 0, 0);

for(int i=0; i<255; i++)
{
    myColor.R--;
    myColor.G++
}

Red and Green make yellow, so higher numbers of red will make it redder, higher numbers of green will make it greener. same of both numbers make it redder.

You can change color incrementally in other ways too, so long as you know how the primary colors of light work.
You're never ever going to find a function called Color.MakeRedder() or Color.MakeGreener() It will always focus on some sort of mathmatical representation of the color, (RBG is most common but there are other representations)

If you want to convert Hue to RBG Here is a guide on how to do it

What would probably be easiest is to keep track of a System.Drawing.Color class as your base color class, and modify your XNA Color Class based on your System.Drawing.Color class accordingly.

If you want to get really adventurous, you can see if it is possible to make a class that extends(inherits from) Microsoft.Xna.Framework.Color, override the R, G, and B properties, so that they are based off of an underlying System.Drawing.Color object

like image 37
Sam I am says Reinstate Monica Avatar answered Feb 22 '23 11:02

Sam I am says Reinstate Monica