Browse Source

implementing color parser

v1.x
Tj Holowaychuk 14 years ago
parent
commit
e2a95c9537
  1. 37
      src/CanvasRenderingContext2d.cc
  2. 18
      src/color.cc

37
src/CanvasRenderingContext2d.cc

@ -17,16 +17,6 @@
Persistent<FunctionTemplate> Context2d::constructor; Persistent<FunctionTemplate> Context2d::constructor;
/*
* Set RGBA.
*/
#define RGBA(_,R,G,B,A) \
_.r = R / 255 * 1; \
_.g = G / 255 * 1; \
_.b = B / 255 * 1; \
_.a = A; \
/* /*
* Rectangle arg assertions. * Rectangle arg assertions.
*/ */
@ -132,9 +122,11 @@ Context2d::Context2d(Canvas *canvas) {
state->globalAlpha = 1; state->globalAlpha = 1;
state->textAlignment = -1; state->textAlignment = -1;
state->fillPattern = state->strokePattern = NULL; state->fillPattern = state->strokePattern = NULL;
RGBA(state->fill,0,0,0,1); rgba_t transparent = { 0,0,0,1 };
RGBA(state->stroke,0,0,0,1); rgba_t transparent_black = { 0,0,0,0 };
RGBA(state->shadow,0,0,0,0); state->fill = transparent;
state->stroke = transparent;
state->shadow = transparent_black;
} }
/* /*
@ -1003,9 +995,12 @@ Context2d::SetStrokePattern(const Arguments &args) {
Handle<Value> Handle<Value>
Context2d::SetShadowColor(const Arguments &args) { Context2d::SetShadowColor(const Arguments &args) {
HandleScope scope; HandleScope scope;
RGBA_ARGS(0); if (!args[0]->IsString()) return Undefined();
String::AsciiValue str(args[0]);
uint32_t rgba = rgba_from_string(*str);
if (!rgba) return Undefined();
Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This()); Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This());
RGBA(context->state->shadow,r,g,b,a); context->state->shadow = rgba_create(rgba);
return Undefined(); return Undefined();
} }
@ -1018,10 +1013,11 @@ Context2d::SetFillColor(const Arguments &args) {
HandleScope scope; HandleScope scope;
if (!args[0]->IsString()) return Undefined(); if (!args[0]->IsString()) return Undefined();
String::AsciiValue str(args[0]); String::AsciiValue str(args[0]);
rgba_t color = rgba_create(rgba_from_string(*str)); uint32_t rgba = rgba_from_string(*str);
if (!rgba) return Undefined();
Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This()); Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This());
context->state->fillPattern = NULL; context->state->fillPattern = NULL;
//RGBA(context->state->fill,r,g,b,a); context->state->fill = rgba_create(rgba);
return Undefined(); return Undefined();
} }
@ -1032,10 +1028,13 @@ Context2d::SetFillColor(const Arguments &args) {
Handle<Value> Handle<Value>
Context2d::SetStrokeColor(const Arguments &args) { Context2d::SetStrokeColor(const Arguments &args) {
HandleScope scope; HandleScope scope;
RGBA_ARGS(0); if (!args[0]->IsString()) return Undefined();
String::AsciiValue str(args[0]);
uint32_t rgba = rgba_from_string(*str);
if (!rgba) return Undefined();
Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This()); Context2d *context = ObjectWrap::Unwrap<Context2d>(args.This());
context->state->strokePattern = NULL; context->state->strokePattern = NULL;
RGBA(context->state->stroke,r,g,b,a); context->state->stroke = rgba_create(rgba);
return Undefined(); return Undefined();
} }

18
src/color.cc

@ -25,7 +25,7 @@
NAME += *str++ - '0'; \ NAME += *str++ - '0'; \
} while (*str >= '0' && *str <= '9'); \ } while (*str >= '0' && *str <= '9'); \
} else { \ } else { \
return -1; \ return 0; \
} \ } \
while (' ' == *str || ',' == *str) str++; while (' ' == *str || ',' == *str) str++;
@ -227,10 +227,10 @@ h(char c) {
rgba_t rgba_t
rgba_create(uint32_t rgba) { rgba_create(uint32_t rgba) {
rgba_t color; rgba_t color;
color.r = rgba >> 24; color.r = (double) (rgba >> 24) / 255;
color.g = (rgba & 0x00ff0000) >> 16; color.g = (double) ((rgba & 0x00ff0000) >> 16) / 255;
color.b = (rgba & 0x0000ff00) >> 8; color.b = (double) ((rgba & 0x0000ff00) >> 8) / 255;
color.a = rgba & 0xff; color.a = (double) (rgba & 0xff) / 255;
return color; return color;
} }
@ -297,7 +297,7 @@ rgba_from_rgb_string(const char *str) {
CHANNEL(b); CHANNEL(b);
return rgba_from_rgb(r, g, b); return rgba_from_rgb(r, g, b);
} }
return -1; return 0;
} }
/* /*
@ -330,7 +330,7 @@ rgba_from_rgba_string(const char *str) {
} }
return rgba_from_rgba(r, g, b, a * 255); return rgba_from_rgba(r, g, b, a * 255);
} }
return -1; return 0;
} }
/* /*
@ -346,7 +346,7 @@ rgba_from_hex_string(const char *str) {
size_t len = strlen(str); size_t len = strlen(str);
if (6 == len) return rgba_from_hex6_string(str); if (6 == len) return rgba_from_hex6_string(str);
if (3 == len) return rgba_from_hex3_string(str); if (3 == len) return rgba_from_hex3_string(str);
return -1; return 0;
} }
/* /*
@ -361,7 +361,7 @@ rgba_from_name_string(const char *str) {
if (*str == *color.name && 0 == strcmp(str, color.name)) if (*str == *color.name && 0 == strcmp(str, color.name))
return color.val; return color.val;
} }
return -1; return 0;
} }
/* /*

Loading…
Cancel
Save