Reputation: 11
I have a project involving an LED module that uses a 3-colour LED which can be controlled by passing in an RGB hex colour, eg 22AAFF
How could I cycle from 000000
to FFFFFF
if I start with this?
long color = 0x000000;
I want to have a loop that does every iteration and displays every possible colour, to end up with
color = 0xFFFFFF
I'm not sure if my understanding of the hex representation of colours makes sense!?
Upvotes: 1
Views: 7757
Reputation:
I think you don't want to loop through All colors but through HUE channel in HSV(HSB) color model. If this is so, you may google for the implementation of the function that converts HSV value to RGB. And you code will look like this:
for(int hue=0; hue<360; hue++)
setColorHSV(hue,1,1);
One of the possible implementations you can find here.
If you don't want to bother with this function you can use this dummy loops:
unsigned int i;
for(i = 0; i <= 0xFF; i++)
{
rgb = 0xFF0000 | i<<8;
setLed(rgb);
}
while(--i > 0)
{
rgb = 0x00FF00 | i<<16;
setLed(rgb);
}
for(i = 0; i <= 0xFF; i++)
{
rgb = 0x00FF00 | i;
setLed(rgb);
}
while(--i > 0)
{
rgb = 0x0000FF | i<<8;
setLed(rgb);
}
for(i = 0; i <= 0xFF; i++)
{
rgb = 0x0000FF | i<<16;
setLed(rgb);
}
while(--i > 0)
{
rgb = 0xFF0000 | i;
setLed(rgb);
}
Unfortunately, I can't test it, but I think it should work without problems. And of course you can optimize it as you want.
Upvotes: 4
Reputation: 11922
You can do it like this
for (int color = 0x000000; color <= 0xFFFFFF; ++color)
But as it was mentioned in the comments, this will take a long time to display all the 16 million colors not to mention that the leds can be unable to display all these colors, so you will probably want something like this:
const int step = 8; // Select this to be a power of two if you want the maximum brightness to be reachable
for( int red = 0x00; red <= 0xFF; red += step ) {
for( int green = 0x00; green <= 0xFF; green += step ) {
for( int blue = 0x00; blue <= 0xFF; blue += step ) {
const int color = blue << 16 + green << 8 + red;
// Change the led settings here.
}
}
}
Upvotes: 6
Reputation: 179991
What's wrong with the obvious for (int color = 0x000000; color <= 0xFFFFFF; ++color)
?
Upvotes: 12