Sorting RGB values by color

I’ve made a small utility to make a lookup texture based on a texture map of a real space object picture.

This works great for a grayscale image, but not so much for an RGB one.

I’ve tried sorting based on different channel, R, G or B, then I tried sorting each color in succession, but there’s always some color messing the gradient. I understand that this is mainly due to the way RGB “wave” goes. I’m wondering if there’s anything out there that anyone of you guys know about that would sort based on color (like group shades of green together for example), while maintaining a colorly order.

I guess I could convert the RGB to HSV, that should help.

1 Like

not entirely sure what you wan’t to achieve, but converting to HSV and then sort on H will probably work as you want :slight_smile:

See it as a palette making utility based on an existing picture.

I sample a picture’s colors pixel by pixel and save the result in a gradient-like LUT.

Here’s a grayscale version of the moon:

Then a “gradient” of Mars. See how it’s broken up at some places?

1 Like

I think we’re more curious “why” you are doing this. It sounds interesting.

Anyway, I don’t think it’s possible to sort a 3D space (RGB) into 1D order and not have some odd clumps randomly in there. For example, if you convert to HSV and sort on H then you have odd clumps of darkness. Even if you sub-sort S and V, there will be odd brightness clashes.

Though I half wonder what Singular Value Decomposition would do to a color space, I’ve never tried it. :slight_smile:

@pspeed said:
I think we're more curious "why" you are doing this. It sounds interesting.

I'll be using the generated LUT to better texture the planets using the procedural texturing. Concisely, I want to use real planetary colors instead of randomly choosing myself. That way the color shades should be more lifelike. So far the result is interesting...

In the end I think there's so many interpolations of colors (~16 million) that I think I'll have to revert to using only one channel. Since there's only 256 shades of gray generated by the noise function, it'd be useless to use anything higher... A bit sad about that.
1 Like


Yeah, I was just curious if it was anything like color reduction… since I know a few ways to do that. Though that might be a nice way to reduce a full color image to 256 colors, I don’t know if it ends up helping at all.

Color space problems are fun.

I think your best bet is to convert to HSL. I guess you want to sort based on lightness in this case.

1 Like

Thanks for that @rhadamanthus

Maybe I’ll revisit that sometimes, but as I said above, with over 16 million possible colors, the exercise is slightly futile for my need which is 256 colors. Of course I could extrapolate to accommodate for more colors but in the end it comes down to 256 shades of gray being mapped. Besides, the result is sometimes interesting with a stray color. :wink:

1 Like