Help with the math on this?

Started by
23 comments, last by Ectara 9 years, 9 months ago

I am not sure how to go about setting up a method to determine a color value for each object that is unique. Obviously they need to be unique to determine which one you clicked on, but with only RGB 256 each channel just doing red++ for 256 values and moving to green++ is one way but how can I code it so it a rolling increment.

e.g.

start with red = 0, red++ when red hits 255 move to green, when green hits 255, blue is incremented to 255 then after that start with red = 1, green = 1, blue = 1 or other combos green = 1, blue = 1 red = 0?

Not sure I am making myself clear, but starting to think ray casting and AABB would just be easier in the long run?

THanks!

For unique colors, you can simply have the color be a 32bit number, and add one when you need a new one. To extract the color, take the bottom eight bits as red, the next lowest eight bits as green, then the eight bits above that as blue. If you want the exact scheme as you've posted, that will require some more logic, but this is the fastest way.

Advertisement

I don't know enough to make it work, but in pseudocode

static int _x = 0;

int[red, green, blue, alpha] GetNextColor()

{

//note, after 2^32 colors, this will fail. if you remove alpha, you only get 2^24 colors

_x++;

int alpha (_x >> 24) % 256 //Only take the last 8 bits

int red = (_x >> 16) % 256 //I think this means that you will ignore the first 16 bits of _x, and only look at the next 8 bits

int green = (_x >> 8) % 256 //Ignore the first 8 bits, and only look at the next 8 bits

int blue = _x % 256 //Only look at the first 8 bits

return [red, green, blue, alpha]

}

I don't understand why some of you developers are so against 'generalisations', that is what you do all the time anyway.

I don't think anyone's against generalizing, per se. Personally, I feel that over-complicating the situation is adding nothing significant; otherwise, we could provide all sorts of methods to solve simple problems, like trying to tell our children to find the area of a square for their geometry homework by integrating.

However, there's something more to it:

I want to take the value

It sure sounds like he's not doing it out of curiosity, by his wording. It also sounds like he wants to program something to do it; otherwise, why ask, if he could do it by hand, and isn't asking simply to be scholarly?

The thing is, if he doesn't understand the algorithm for what he wants, and he's trying to implement it, then giving him a complex and inefficient way of solving the problem isn't the best answer, if he may be unable to derive the simpler answer for his use case. Linear interpolation is much slower than subtracting two numbers, so it is no better of an answer than the one already posted.

Of course, if Paradigm Shifter was attempting to build on fir's answer with more information for the sake of knowledge, then that's well and fine. However, providing a one line answer that may sound contradictory to someone who doesn't immediately see the link between the two creates confusion. If Paradigm Shifter's first two posts were combined into one, from the beginning, and underscored that the simplification was what the OP was after, and how we got there, there probably would have been less conflict.

All in all, with clarification in place, I agree with the above points.

That is not what I meant with my post. The one line answer is good and correct and sane. The LERP answer is overly complicated, but... My point is that even if he got his answer, which I am sure is how everybody would solve this problem, it is NOT useless to fill in more information about a topic. If you don't want to learn from it, that is fine, but my point was to tell naysayers here to not dismiss on behalf of everybody else who want to learn something new, and be better at what they are doing. This thread is available for anyone to learn from for the future. To add information that people can learn from if they want, is in my opinion not a bad thing, it is actually good. That is the reason I defended the person who wrote the little note about LERP. If you are not interested? That is fine. I found it interesting, and someone else might find it interesting too. This forum is full of people giving more information about things than is initially asked for. Lots of people are learning from that.

I don't know enough to make it work, but in pseudocode

static int _x = 0;

int[red, green, blue, alpha] GetNextColor()

{

//note, after 2^32 colors, this will fail. if you remove alpha, you only get 2^24 colors

_x++;

int alpha (_x >> 24) % 256 //Only take the last 8 bits

int red = (_x >> 16) % 256 //I think this means that you will ignore the first 16 bits of _x, and only look at the next 8 bits

int green = (_x >> 8) % 256 //Ignore the first 8 bits, and only look at the next 8 bits

int blue = _x % 256 //Only look at the first 8 bits

return [red, green, blue, alpha]

}

I am not sure what you are looking to do here? Is this allowing me to add one to the color palette and cycle all the way through rgb? so get all 16.7 million color variations?

If so thanks!

Update!

BedderDanu thanks that worked great!!!


If you are not interested?

No one said I wasn't interested. To be honest, I never thought of it that way.


it is NOT useless to fill in more information about a topic.

Never said that.


Of course, if Paradigm Shifter was attempting to build on fir's answer with more information for the sake of knowledge, then that's well and fine.

I have no idea where you are getting the idea that I am against his insight.

This topic is closed to new replies.

Advertisement