Neotrellis Colour asign.

Hi All.

Im trying to asign the colour to a trellis button.

I can do it this way.

trellis.setPixelColor(3, 5, 0x000000);
trellis.show();

My question is: Can i asing the colour with the RGB code instead of hexadecimal ?

I mean, i try to just put the 3 digitis but no luck

Thanks

Moris526:
I mean, i try to just put the 3 digitis but no luck

I don’t know what you are using - Don’t post snippets (Snippets R Us!) - but if your class does not offer a method with the 3 parameters then no you can’t directly and have to write your own.

It’s not difficult if you have three byte RR GG and BB to build up with an uint32_t which is 0x00RRGGBB that you can use to call the existing method (if this is the expected color coding)

Hi,
What library and model Arduino are you using?
What is a trellis button, please post link/data.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Thanks for the answer. Sorry for the lack of info.

Im using "Adafruit_NeoTrellis.h" Library

setPixelColor (uint16_t num, uint32_t color); // number of pixel on grid, and colour

Then, I should make my 3 RGB integers to uint32_t right?

any simple way?

Moris526:
Then, I should make my 3 RGB integers to uint32_t right?

does this help?

inline uint32_t rgbColor(uint32_t r, uint32_t g, uint32_t b)
{
  return ((r << 16) | (g << 8) | b);
}

void setup() {
  Serial.begin(115200);
  Serial.print("rgbColor(0xAA, 0xBB, 0xCC) ==> 0x");
  Serial.println(rgbColor(0xAA, 0xBB, 0xCC), HEX);
}

void loop() {}

Yes !!!

Thank you !!!!

:slight_smile:

Here’s something I made up a while ago that you might find useful. It’s all 140 of the HTML named colors, in RGB format.

For your purposes, you’d use the ones in the RGB888 namespace. It also include the 16 bit (RGB 565) versions that are commonly used on Arduino displays.

HtmlColors.h (22 KB)

If you look at some of the examples for the Adafruit_NeoTrellis library, you will find that Adafruit_NeoPixel::Color() can also be used, although that is a bit longer to type than the rgbColor() function posted above. The Color() function is basically identical, except for the arguments being 8-bit.

  static uint32_t   Color(uint8_t r, uint8_t g, uint8_t b) {
    return ((uint32_t)r << 16) | ((uint32_t)g <<  8) | b;
  }

david_2018:
except for the arguments being 8-bit.

that was me being lazy to cast the parameters to (uint32_t) before shifting and letting the compiler do the work :slight_smile:

(only issue is if you call the function with parameters that are > 255 then you'll end up with some crappy colors. forcing the parameters to be uint8_t prune the excess bits before entering the function and then the cast and shift will do the right thing)

J-M-L:
does this help?

inline uint32_t rgbColor(uint32_t r, uint32_t g, uint32_t b)

{
  return ((r << 16) | (g << 8) | b);
}

void setup() {
  Serial.begin(115200);
  Serial.print(“rgbColor(0xAA, 0xBB, 0xCC) ==> 0x”);
  Serial.println(rgbColor(0xAA, 0xBB, 0xCC), HEX);
}

void loop() {}

Sorry. I dont know how to apply that.

Lets say I have

int r
int g
int b

how do I get to the colour in 0x000000 format…?

You don’t neee r,g nor.b to be integers - that’s 2 bytes each and you only can have a value between 0 (Off) and 255 (full on) for each component so they should be byte type (or uint8_t)

When you need to pass a color as a 32 bit value 0x00RRGGBB you just call the function

 trellis.setPixelColor(X, Y, rgbColor(r,g,b));
trellis.show();

There I go !!!

Thank you for the patience.

Have a nice day!!!

J-M-L:
You don’t need r,g nor.b to be integers "int"s - that’s 2 bytes each and you only can have a value between 0 (Off) and 255 (full on) for each component so they should be byte type (or uint8_t)

indeed int-s was what I meant