Question

Imagine round color pallet like this.

I am building a game(Android) in which you can draw line and when you drawing it you're creating vector between each two points. I need to transform this direction to color on the pallet. So when you draw, the color of the line will depend on your swipe direction. I need to transform color to direction too. Can't figure out relation between 3 rgb params and x,y direction. Of course I could limit number of colors to, for example 8. Then create array and every cell will represent direction of different color. But I wonder if it possible to use all of the colors without allocating tons of memory or using unnecessary if\else's?

Update: Thanks to domi's advice I was able to do what I wanted. Thanx man!

That's how I converted vector to color:

double angle = Math.atan2(vector_x, vector_y) * 57.2957795;
double final_angle = angle<0? 360 + angle:angle;
int myRGBColor = Color.HSVToColor(new float[]{(float) final_angle, saturation, brightness} );

And that's how I converted color to vector:

int sample = bmp.getPixel( (int)X, (int)Y);
//int a = (sample >> 24) & 255;
int r = (sample >> 16) & 255;
int g = (sample >> 8) & 255;
int b = sample & 255;

float[] hsv = new float[3];
android.graphics.Color.RGBToHSV(r, g, b, hsv);
float hue = hsv[0]; 
vector_x =  Math.toDegrees( Math.sin(  Math.toRadians(hue) ) );
vector_y =  Math.toDegrees( Math.cos(  Math.toRadians(hue) ) );

Everyone is welcome to play my game.

Was it helpful?

Solution

If you have a vector you can use the degree to calculate the color with the HSL system. If you go on the round color pallet you can see that Saturation (S) is always 100 and Luminance (L) is always 50 then you just have to use the degree (0-360) to have the HUE and have a complete HSL color (degree, 100, 50). Then you can transforme RGB to HSL and HSL to RGB

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top