Last active
January 8, 2022 15:49
-
-
Save mofosyne/b1fc240b64c520c0bf3541a029e3dcc3 to your computer and use it in GitHub Desktop.
Parse web color hexes for pallet (e.g. `0xFFAD63, ...`) (e.g. `#FFAD63, ...`)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
int palletColorParse(uint32_t *palletColor, const int palletColorSize, const char * parameterStr) | |
{ | |
// Parse web color hexes for pallet (e.g. `0xFFAD63, ...`) (e.g. `#FFAD63, ...`) | |
// This was created for the cdecoder in https://github.com/mofosyne/arduino-gameboy-printer-emulator | |
// https://gist.github.com/mofosyne/b1fc240b64c520c0bf3541a029e3dcc3 | |
// Brian Khuu 2021 | |
if (!parameterStr) | |
return 0; | |
int palletCounter = 0; | |
int nibIndex = 0; | |
uint32_t pallet = 0; | |
char prevChar = 0; | |
for ( ; (*(parameterStr)) != '\0' ; parameterStr++) | |
{ | |
const char ch = *parameterStr; | |
// Search for start of #XXXXXX, we are looking for 4 pallets | |
if (nibIndex == 0) | |
{ | |
if ((ch == '#') || ((prevChar == '0')&&(ch == 'x'))) | |
{ | |
pallet = 0; | |
nibIndex = 3*2; // [R, G, B] | |
prevChar = 0; | |
continue; | |
} | |
prevChar = ch; | |
continue; | |
} | |
nibIndex--; | |
// Parse Nibble | |
char nib = -1; | |
if (('0' <= ch) && (ch <= '9')) | |
nib = ch - '0'; | |
else if (('a' <= ch) && (ch <= 'f')) | |
nib = ch - 'a' + 10; | |
else if (('A' <= ch) && (ch <= 'F')) | |
nib = ch - 'A' + 10; | |
else | |
{ | |
nibIndex = 0; | |
palletColor[palletCounter] = pallet; | |
palletCounter++; | |
if (palletCounter >= palletColorSize) | |
break; | |
continue; | |
} | |
// Pallet | |
pallet |= nib << (nibIndex*4); | |
if (nibIndex == 0) | |
{ | |
palletColor[palletCounter] = pallet; | |
palletCounter++; | |
if (palletCounter >= palletColorSize) | |
break; | |
} | |
} | |
return palletCounter; | |
} | |
void test(char *str) | |
{ | |
uint32_t palletColor[4] = {0,0,0,0}; | |
int count = palletColorParse(palletColor, sizeof(palletColor)/sizeof(palletColor[0]), str); | |
printf("0x%06X, 0x%06X, 0x%06X, 0x%06X (parseCount %d) <-- '%s'\n", palletColor[0], palletColor[1], palletColor[2], palletColor[3], count, str); | |
} | |
int main (void) | |
{ | |
/* | |
0xFFFFFF, 0xFFAD63, 0x833100, 0x000000 (parseCount 4) <-- '0xffffff, 0xffad63, 0x833100, 0x000000' | |
0xFFFFFF, 0xFFAD63, 0x833100, 0x000000 (parseCount 4) <-- '#ffffff#ffad63#833100#000000' | |
0xFFFFFF, 0xFFAD63, 0x833100, 0x000000 (parseCount 4) <-- '#ffffff, #ffad63, #833100, #000000' | |
0xFAFAFA, 0xFAFAFA, 0xFAFAFA, 0xFAFAFA (parseCount 4) <-- '#fafafa #fafafa #fafafa #fafafa #fafafa' | |
0xFFFFFF, 0xFFAD63, 0x833100, 0x000000 (parseCount 4) <-- '#ffffffff, #ffad63aa, #83310066, #00000088' | |
0xFFFF00, 0xFFAD00, 0x833100, 0x000000 (parseCount 3) <-- '#ffff, #ffad, #8331, #0000' | |
0xFF0000, 0xFF0000, 0x830000, 0x000000 (parseCount 3) <-- '#ff, #ff, #83, #00' | |
*/ | |
test("0xffffff, 0xffad63, 0x833100, 0x000000"); | |
test("#ffffff#ffad63#833100#000000"); | |
test("#ffffff, #ffad63, #833100, #000000"); | |
test("#fafafa #fafafa #fafafa #fafafa #fafafa"); | |
test("#ffffffff, #ffad63aa, #83310066, #00000088"); | |
test("#ffff, #ffad, #8331, #0000"); | |
test("#ff, #ff, #83, #00"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment