RGB to 5:6:5 Hex color converter

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
jersagfast
 
Posts: 83
Joined: Mon May 16, 2011 1:10 pm

RGB to 5:6:5 Hex color converter

Post by jersagfast »

Hi guys and gals, does anyone know of a good program or formula to convert RGB values (or CMYK) to the 16 bit 5:6:5 hex format? I have a ton that convert RGB to hex, just not in the 5:6:5 format. I have searched and found very little, and would be estatic with even just a formula. Thanks kindly. :D

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: RGB to 5:6:5 Hex color converter

Post by adafruit »

the bmp viewer has a converter from 888 to 565

User avatar
jersagfast
 
Posts: 83
Joined: Mon May 16, 2011 1:10 pm

Re: RGB to 5:6:5 Hex color converter

Post by jersagfast »

Thanks! But is there a way to convert single colors to define them in my sketch? IE: #define COLORXYZ 0x1CB6. I'm not displaying bitmaps, but building a menu system on the 2.8 TFT/touch screen. I have searched this process/formula and peaked in the 2.8TFT library and found you need to shift 3 something, 11 something, and then I get lost. I am now motivated to write a sketch for this conversion and post it for other noobs like myself. :oops: Almost time for ask an engineer! I promise I won't bring this up tonight.. :wink:

User avatar
tastewar
 
Posts: 369
Joined: Thu Mar 17, 2011 10:16 am

Re: RGB to 5:6:5 Hex color converter

Post by tastewar »

I confess that I don't know what "the bmp viewer" is (though I can guess what it does), but perhaps you are being steered to the source code for it to see how it's done...

User avatar
jersagfast
 
Posts: 83
Joined: Mon May 16, 2011 1:10 pm

Re: RGB to 5:6:5 Hex color converter

Post by jersagfast »

I believe the BMP viewer is the sample sketch that comes with the TFTLCD library. It will convert a bitmap image, but I need to define single colors. The entire library with the source code can be downloaded here:
https://github.com/adafruit/TFTLCD-Library
Hope this helps.

User avatar
tastewar
 
Posts: 369
Joined: Thu Mar 17, 2011 10:16 am

Re: RGB to 5:6:5 Hex color converter

Post by tastewar »

Excellent. Sounds like it would be a perfect source for the code you need. Have you looked at the source code? Have you tried to figure out where it converts colors from 8/8/8 to 5/6/5?

User avatar
jersagfast
 
Posts: 83
Joined: Mon May 16, 2011 1:10 pm

Re: RGB to 5:6:5 Hex color converter

Post by jersagfast »

I have looked at it, but I don't quite understand it. when I figure it out, I will write a sketch for that single function and post it. :)

User avatar
ktownsend
 
Posts: 1447
Joined: Thu Nov 05, 2009 2:18 am

Re: RGB to 5:6:5 Hex color converter

Post by ktownsend »

Here is a C function that converts 8-bit R/G/B into RGB565:

Code: Select all

/**************************************************************************/
/*!
    @brief  Converts a 24-bit RGB color to an equivalent 16-bit RGB565 value

    @param[in]  r
                8-bit red
    @param[in]  g
                8-bit green
    @param[in]  b
                8-bit blue

    @section Example

    @code 

    // Get 16-bit equivalent of 24-bit color
    uint16_t gray = drawRGB24toRGB565(0x33, 0x33, 0x33);

    @endcode
*/
/**************************************************************************/
uint16_t drawRGB24toRGB565(uint8_t r, uint8_t g, uint8_t b)
{
  return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
}
And although you didn't ask for it, a function to convert RGB565 back to a standard 24-bit BGRA value:

Code: Select all

/**************************************************************************/
/*!
    @brief  Converts a 16-bit RGB565 color to a standard 32-bit BGRA32
            color (with alpha set to 0xFF)

    @param[in]  color
                16-bit rgb565 color

    @section Example

    @code 

    // First convert 24-bit color to RGB565
    uint16_t rgb565 = drawRGB24toRGB565(0xFF, 0x00, 0x00);
  
    // Convert RGB565 color back to BGRA32
    uint32_t bgra32 = drawRGB565toBGRA32(rgb565);
  
    // Display results
    printf("BGRA32: 0x%08X R: %u G: %u B: %u A: %u \r\n", 
        bgra32, 
        (bgra32 & 0x000000FF),        // Blue
        (bgra32 & 0x0000FF00) >> 8,   // Green
        (bgra32 & 0x00FF0000) >> 16,  // Red
        (bgra32 & 0xFF000000) >> 24); // Alpha

    @endcode
*/
/**************************************************************************/
uint32_t drawRGB565toBGRA32(uint16_t color)
{
  uint32_t bits = (uint32_t)color;
  uint32_t blue = bits & 0x001F;     // 5 bits blue
  uint32_t green = bits & 0x07E0;    // 6 bits green
  uint32_t red = bits & 0xF800;      // 5 bits red

  // Return shifted bits with alpha set to 0xFF
  return (red << 8) | (green << 5) | (blue << 3) | 0xFF000000;
}

User avatar
jersagfast
 
Posts: 83
Joined: Mon May 16, 2011 1:10 pm

Re: RGB to 5:6:5 Hex color converter

Post by jersagfast »

Thanks ktownsend! I used one of the line from that, and it works now! :D I'll put the code below for anyone who wants to use it, it should work on Lady Ada's new touch shield, as it is based off of the TFT Paint sketch. I put a Youtube video of how it works for anyone wanting a preview before building. The wiring is the same as the 2.8" TFT tutorial found here:http://www.ladyada.net/products/tfttouchbreakout/
The youtube video can be found here:http://www.youtube.com/watch?v=mFgouSrsiYQ
Thanks Lady Ada and ktownsend!
Forgot to mention, I did put the backlight on pin 10 for pwm control.
Is it wrong to feel good about posting code that works? :) -Glad to give my part when I can.
Code:

Code: Select all

#include "TFTLCD.h"
#include "TouchScreen.h"
/* For the 8 data pins:
 Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
 D0 connects to digital 8
 D1 connects to digital 9
 D2 connects to digital 2
 D3 connects to digital 3
 D4 connects to digital 4
 D5 connects to digital 5
 D6 connects to digital 6
 D7 connects to digital 7
 For Mega's use pins 22 thru 29 (on the double header at the end)
 */
#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0 
// optional
#define LCD_RESET A4
// Color definitions
#define	BLACK           0x0000
#define	BLUE            0x001F
#define	RED             0xF800
#define	GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0 
#define WHITE           0xFFFF
#define JJCOLOR         0x1CB6
TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
int i = 0;
int backlight = 10;
int blv = 255;
long r;
long g;
long b;
char red[10];
char green[10];
char blue[10];
char output[10];
unsigned long answer;
void setup(void) {
  pinMode(backlight, OUTPUT);
  Serial.begin(9600);
  tft.setRotation(1);
  tft.reset();
  uint16_t identifier = tft.readRegister(0x0);
  if (identifier == 0x9325) {
    Serial.println("Found ILI9325 controller.");
  } 
  else if (identifier == 0x9328) {
    Serial.println("Found ILI9328 controller.");
  } 
  else {
    Serial.print("Unknown driver chip ");
    Serial.println(identifier, HEX);
    while (1);
  }
  tft.initDisplay();
  tft.fillScreen(BLACK);
  tft.fillRect(71, 70, 50, 100, JJCOLOR);
  tft.fillRect(134, 70, 50, 100, JJCOLOR);
  tft.fillRect(197, 70, 50, 100, JJCOLOR);
  tft.drawRect(46, 45, 228, 150, WHITE);
  for(i = 0 ; i <= blv; i+=1) { 
    analogWrite(10, i);
    delay(2);
  }
  delay(250);
  tft.drawString(85, 100, "J", WHITE, 5);
  delay(250);
  tft.drawString(147, 100, "O", WHITE, 5);
  delay(250);
  tft.drawString(210, 100, "S", WHITE, 5);
  delay(500);
  tft.drawString(84, 210, "Jeremy Saglimbeni (c) 2011", WHITE);
  tft.drawString(123, 230, "jandjpro.com", WHITE);
  delay(500);
  tft.drawString(108, 20,"Thanks ktownsend!!!", WHITE);
  delay(1500);
  tft.fillScreen(BLACK);
  drawscreen();
  showred();
  showgrn();
  showblu();
  tft.fillRect(0, 0, 320, 10, JJCOLOR);
  tft.drawString(247, 223, "0x", WHITE, 2);
  tft.drawRect(244, 220, 76, 20, WHITE);
  tft.drawString(252, 207, "HEX Output", WHITE);
  tft.drawString(60, 207, "Preview Swatch", WHITE);
  tft.drawString(60, 217, "(Press to reset)", WHITE);
  showhex();
  tft.drawString(1, 1, "8:8:8 RGB to 5:6:5 HEX Color Converter  -  jersagfast", WHITE);
}
#define MINPRESSURE 10
#define MAXPRESSURE 1000
void loop() {
  Point p = ts.getPoint();
  // if you're sharing pins, you'll need to fix the directions of the touchscreen pins!
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);
  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    /*
    Serial.print("X = "); 
     Serial.print(p.x);
     Serial.print("\tY = "); 
     Serial.print(p.y);
     Serial.print("\tPressure = "); 
     Serial.println(p.z);
     */
    // turn from 0->1023 to tft.width
    p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);
    /*
    Serial.print("("); 
     Serial.print(p.x);
     Serial.print(", "); 
     Serial.print(p.y);
     Serial.println(")");
     */
    // red buttons
    if (p.y > 3 && p.y < 65 && p.x > 210 && p.x < 264) {
      r = r - 1;
      if (r <= 0) {
        r = 0;
      }
      showred();
      showhex();
    }
    if (p.y > 269 && p.y < 324 && p.x > 210 && p.x < 264) {
      r = r + 1;
      if (r >= 255) {
        r = 255;
      }
      showred();
      showhex();
    }
    // green buttons
    if (p.y > 3 && p.y < 65 && p.x > 139 && p.x < 195) {
      g = g - 1;
      if (g <= 0) {
        g = 0;
      }
      showgrn();
      showhex();
    }
    if (p.y > 269 && p.y < 324 && p.x > 139 && p.x < 195) {
      g = g + 1;
      if (g >= 255) {
        g = 255;
      }
      showgrn();
      showhex();
    }
    // blue buttons
    if (p.y > 3 && p.y < 66 && p.x > 72 && p.x < 126) {
      b = b - 1;
      if (b <= 0) {
        b = 0;
      }
      showblu();
      showhex();
    }
    if (p.y > 269 && p.y < 324 && p.x > 72 && p.x < 126) {
      b = b + 1;
      if (b >= 255) {
        b = 255;
      }
      showblu();
      showhex();
    }
    // reset button
    if (p.y > 1 && p.y < 55 && p.x > 12 && p.x < 57) {
      r = 0;
      g = 0;
      b = 0;
      showred();
      showgrn();
      showblu();
      showhex();
    }
    /*
    // uncomment this to get serial output when screen is pressed
    Serial.print("r:");
    Serial.print(r);
    Serial.print("   g:");
    Serial.print(g);
    Serial.print("   b:");
    Serial.println(b);
    Serial.print("HEX Output:0x");
    Serial.println(answer, HEX);
    */
  }
}
void showred() {
  itoa (r, red, 10);
  tft.fillRect(144, 45, 36, 16, BLACK);
  tft.drawString(144, 45, red, WHITE, 2);
}
void showgrn() {
  itoa (g, green, 10);
  tft.fillRect(144, 105, 36, 16, BLACK);
  tft.drawString(144, 105, green, WHITE, 2);
}
void showblu() {
  itoa (b, blue, 10);
  tft.fillRect(144, 165, 36, 16, BLACK);
  tft.drawString(144, 165, blue, WHITE, 2);
}
void showhex() {
  answer = ((r / 8) << 11) | ((g / 4) << 5) | (b / 8); //Thanks ktownsend for this!
  tft.fillRect(0, 200, 50, 40, answer);
  itoa (answer, output, HEX);
  tft.fillRect(271, 223, 46, 16, BLACK);
  tft.drawString(271, 223, output, WHITE, 2); 
}
void drawscreen() {
  // Red
  tft.fillRect(0, 20, 60, 50, RED);
  tft.drawRect(0, 20, 60, 50, WHITE);
  tft.drawRect(80, 20, 160, 50, JJCOLOR);
  tft.fillRect(260, 20, 60, 50, GREEN);
  tft.drawRect(260, 20, 60, 50, WHITE);
  tft.drawString(22, 33, "-", WHITE, 3);
  tft.drawString(134, 31, "RED Value", RED);
  tft.drawString(282, 33, "+", WHITE, 3);
  //Green
  tft.fillRect(0, 80, 60, 50, RED);
  tft.drawRect(0, 80, 60, 50, WHITE);
  tft.drawRect(80, 80, 160, 50, JJCOLOR);
  tft.fillRect(260, 80, 60, 50, GREEN);
  tft.drawRect(260, 80, 60, 50, WHITE);
  tft.drawString(22, 93, "-", WHITE, 3);
  tft.drawString(128, 91, "Green Value", GREEN);
  tft.drawString(282, 93, "+", WHITE, 3);
  //Blue
  tft.fillRect(0, 140, 60, 50, RED);
   tft.drawRect(0, 140, 60, 50, WHITE);
   tft.drawRect(80, 140, 160, 50, JJCOLOR);
   tft.fillRect(260, 140, 60, 50, GREEN);
   tft.drawRect(260, 140, 60, 50, WHITE);
   tft.drawString(22, 153, "-", WHITE, 3);
   tft.drawString(130, 151, "Blue Value", BLUE);
   tft.drawString(282, 153, "+", WHITE, 3);
}

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: RGB to 5:6:5 Hex color converter

Post by adafruit »

wow nice work! glad our new library is making all this possible :)

User avatar
jersagfast
 
Posts: 83
Joined: Mon May 16, 2011 1:10 pm

Re: RGB to 5:6:5 Hex color converter

Post by jersagfast »

Very much so! Thanks a bunch - I heart landscape! :)
Made this work also:http://www.youtube.com/watch?v=kub3FsfFNwA
Need to buy more TFT screens..

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: RGB to 5:6:5 Hex color converter

Post by adafruit »

wow, intense! please let us know when you have your project done - if you put up a video and post your code we can send you a gift! (just post when its complete)

User avatar
jersagfast
 
Posts: 83
Joined: Mon May 16, 2011 1:10 pm

Re: RGB to 5:6:5 Hex color converter

Post by jersagfast »

I would be glad to! I planned on making this a "generic" "you fill in the labels and actions" open "operating system". That's a lot of "quotes". I'll post all code when I'm done. I'm using your XBee adaptors to talk to my home automation system. Works great! Would love to see others use/hack/modify this!

User avatar
brooksware2000
 
Posts: 6
Joined: Sun Oct 17, 2010 10:20 pm

Re: RGB to 5:6:5 Hex color converter

Post by brooksware2000 »

Hello everyone, this is my first post on the forums but, I wanted to post a Visual C# 2010 project that may aid others in creating color patterns in 5:6:5 format. The application is self explanatory and the hex value can be copied and pasted into the Arduino ide. Hope someone finds this useful. I'd like to give credit to ktownsend and jersagfast for their code and project contributions.

Curtis

Image

Application Link

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: RGB to 5:6:5 Hex color converter

Post by adafruit »

nice! very handy!

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”