Skip to content

Instantly share code, notes, and snippets.

@chergert
Created April 4, 2018 11:30
Show Gist options
  • Save chergert/acc7f41c4d5a45ba254188bb19764f72 to your computer and use it in GitHub Desktop.
Save chergert/acc7f41c4d5a45ba254188bb19764f72 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <assert.h>
typedef struct
{
double r;
double g;
double b;
double a;
} GdkRGBA;
# define _GDK_RGBA_DECODE(c) ((unsigned)((c >= 'A' && c <= 'F') ? (c-'A'+10) : (c >= 'a' && c <= 'f') ? (c-'a'+10) : (c >= '0' && c <= '9') ? (c-'0') : -1))
# define _GDK_RGBA_HIGH(c) (_GDK_RGBA_DECODE(c) << 4)
# define _GDK_RGBA_LOW(c) _GDK_RGBA_DECODE(c)
# define _GDK_RGBA(r,g,b,a) ((GdkRGBA){(r)/255.,(g)/255.,(b)/255.,(a)/255.})
# define _GDK_RGBA8(str) \
_GDK_RGBA(_GDK_RGBA_HIGH(str[0]) | _GDK_RGBA_LOW(str[1]), \
_GDK_RGBA_HIGH(str[2]) | _GDK_RGBA_LOW(str[3]), \
_GDK_RGBA_HIGH(str[4]) | _GDK_RGBA_LOW(str[5]), \
_GDK_RGBA_HIGH(str[6]) | _GDK_RGBA_LOW(str[7]))
# define _GDK_RGBA6(str) \
_GDK_RGBA(_GDK_RGBA_HIGH(str[0]) | _GDK_RGBA_LOW(str[1]), \
_GDK_RGBA_HIGH(str[2]) | _GDK_RGBA_LOW(str[3]), \
_GDK_RGBA_HIGH(str[4]) | _GDK_RGBA_LOW(str[5]), \
0xFF)
# define _GDK_RGBA4(str) \
_GDK_RGBA(_GDK_RGBA_HIGH(str[0]) | _GDK_RGBA_LOW(str[0]), \
_GDK_RGBA_HIGH(str[1]) | _GDK_RGBA_LOW(str[1]), \
_GDK_RGBA_HIGH(str[2]) | _GDK_RGBA_LOW(str[2]), \
_GDK_RGBA_HIGH(str[3]) | _GDK_RGBA_LOW(str[3]))
# define _GDK_RGBA3(str) \
_GDK_RGBA(_GDK_RGBA_HIGH(str[0]) | _GDK_RGBA_LOW(str[0]), \
_GDK_RGBA_HIGH(str[1]) | _GDK_RGBA_LOW(str[1]), \
_GDK_RGBA_HIGH(str[2]) | _GDK_RGBA_LOW(str[2]), \
0xFF)
# define GDK_RGBA(str) ((sizeof(str) == 9) ? _GDK_RGBA8(str) : \
(sizeof(str) == 7) ? _GDK_RGBA6(str) : \
(sizeof(str) == 5) ? _GDK_RGBA4(str) : \
(sizeof(str) == 4) ? _GDK_RGBA3(str) : \
((GdkRGBA){0,0,0,0}))
int main (int argc, char *argv[])
{
GdkRGBA rgba = GDK_RGBA("FF0000FF");
GdkRGBA rgba2 = GDK_RGBA("F00F");
assert (rgba.r == 1);
assert (rgba.g == 0);
assert (rgba.b == 0);
assert (rgba.a == 1);
assert (rgba2.r == 1);
assert (rgba2.g == 0);
assert (rgba2.b == 0);
assert (rgba2.a == 1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment