# monochromatic color palette Generation

I am looking for algorithmically generate monochromatic color palette of any color?. what is the algorithm for monochromatic color palette generation?

What you mean by monochromatic, is it from black to white, with only one colour gradient in the middle, like what you see with various tools (ex: paint)?

If so, looking at the numbers in those gives you a big hint.
Iâ€™ll take Paint as an example, because any Windows system have it, but you can take any other tool that has this as a reference.

If you manage to take the exact center of the gradient, you get the colour that is used as the base.

From the base colour code, you then make the gradients with simple math formulas toward both 0,0,0 and 255,255,255 (or 1f, 1f, 1f for C# Color values).

From here, itâ€™s just outside of the question, but still relevant.
It could technically work for any colour, but you will notice that they usually only propose a specific palette, ranging from a pure grey (0.5f, 0.5f, 0.5f) to â€śpureâ€ť colours that have at least one parameter to 1f and at most two non-zero parameters.
Again, between the grey and the â€śpureâ€ť, you have a gradient.

The best part about this technique is that any of those colours are able to give a smooth gradient from black to white, where both sides of the colour have an equal difference for each step.

Well, all of this is a lot of talk about the theory, and not much of implementation, so depending on how you usually work, it might or might not be enough for you.

Iâ€™ll make a short example later if you really canâ€™t figure it out (but it wonâ€™t be a complete script that you can use).

1 Like

I think what you mean is right.
some image ref for what i mean monochromatic color

Just convert between HSV and RGB as needed, and use only the H hue that you want it to be

2 Likes

Take two colors and then use Color.Lerp with them to generate some colors in between.

â€“Eric

1 Like

I donâ€™t think this yields ideal results; Slerp will result in colors of more uniform value:

``````namespace UnityEngine {
public static class ColorExtensions {
public static Vector3 GetHue(this Color color) {return color.GetRGB().normalized;}

public static Vector3 GetRGB(this Color color) {return (Vector3)(Vector4)color;}

public static float GetValue(this Color color) {return color.GetRGB().magnitude;}

public static Color WithRGB(this Color color, Vector3 rgb) {
for (int i = 0; i < 3; i++) color[i] = rgb[i];
return color;
}

public static Color WithSaturation(this Color color, float saturation) {
if (color.r == color.g && color.g == color.b) return color;

Vector3 colorWithSaturation;
var value = color.GetValue();
var colorWithSaturation0 = value * Vector3.one.normalized;
if (saturation == 0) colorWithSaturation = colorWithSaturation0;
else {
Vector3 fullySaturated = (Vector4)color;
var minChannel = color.GetRGB().Min();
for (int i = 0; i < 3; i++)
if (fullySaturated[i] == minChannel) fullySaturated[i] = 0;
fullySaturated = fullySaturated.normalized * value;
colorWithSaturation = Vector3.Slerp(colorWithSaturation0, fullySaturated, saturation);
}
return color.WithRGB(colorWithSaturation);
}
}
}
``````
``````namespace UnityEngine {
public static class Vector3Extensions {
public static float Dot(this Vector3 vectorA, Vector3 vectorB)
{return Vector3.Dot(vectorA,vectorB);}

public static float Min(this Vector3 vector) {
var min = vector.x;
for (int i = 1; i < 3; i++) if (vector[i] < min) min = vector[i];
return min;
}
}
}
``````

With that, you can do something like this:

``````Color color;
Color[] colors;

colors = Enumerable.Range(0, colors.Length)
.Select( i => color.WithSaturation((float)i / (colors.Length - 1)) )
.ToArray();
``````

1 Like