runtime convert HEX colorcode to RGB565 colorcode


hello,

25 years ago coded lot of basic, turbo pascal , visual basic.
but never coded 1 line arduino til now.
this why need started it.

i'm looking small function converts hex colorcode rgb565 colorcode during runtime.

some easy examples:
#000000 should return 0x0000 (black)
#ff0000 should return 0xf800 (red)
#00ff00 should return 0x07e0 (green)
#0000ff should return 0x001f (blue)
#ffffff should return 0xffff (white)

...and on.

the best code found til is:

code: [select]

uint16_t rgb565( const unsigned long rgb)
{
  uint16_t r = (rgb >> 16) & 0xff;
  uint16_t g = (rgb >>  8) & 0xff;
  uint16_t b = (rgb      ) & 0xff;

  uint16_t ret  = (r & 0xf8) << 8;  // 5 bits
           ret |= (g & 0xfc) << 3;  // 6 bits
           ret |= (b & 0xf8) >> 3;  // 5 bits
       
  return( ret);
}

this nice short , easy.
but works not need it.
maybe can adapt me, returns "0x07e0" when gets "#00ff00" ?

thanks , best regards,
chris



maybe can adapt me, returns "0x07e0" when gets "#00ff00" ?
it return 0x7e0... trying convert strings , hex?


Arduino Forum > Community > Gigs and Collaborations > runtime convert HEX colorcode to RGB565 colorcode


arduino

Comments

Popular posts from this blog

Help with codes to add 1 more shift register and add more to matrix LED

tcp client

Is there an HTML component in Flex 2.0?