diff --git a/src/CanvasRenderingContext2d.cc b/src/CanvasRenderingContext2d.cc index e142141..77fb37c 100644 --- a/src/CanvasRenderingContext2d.cc +++ b/src/CanvasRenderingContext2d.cc @@ -17,16 +17,6 @@ Persistent 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. */ @@ -132,9 +122,11 @@ Context2d::Context2d(Canvas *canvas) { state->globalAlpha = 1; state->textAlignment = -1; state->fillPattern = state->strokePattern = NULL; - RGBA(state->fill,0,0,0,1); - RGBA(state->stroke,0,0,0,1); - RGBA(state->shadow,0,0,0,0); + rgba_t transparent = { 0,0,0,1 }; + rgba_t transparent_black = { 0,0,0,0 }; + state->fill = transparent; + state->stroke = transparent; + state->shadow = transparent_black; } /* @@ -1003,9 +995,12 @@ Context2d::SetStrokePattern(const Arguments &args) { Handle Context2d::SetShadowColor(const Arguments &args) { 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(args.This()); - RGBA(context->state->shadow,r,g,b,a); + context->state->shadow = rgba_create(rgba); return Undefined(); } @@ -1018,10 +1013,11 @@ Context2d::SetFillColor(const Arguments &args) { HandleScope scope; if (!args[0]->IsString()) return Undefined(); 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(args.This()); context->state->fillPattern = NULL; - //RGBA(context->state->fill,r,g,b,a); + context->state->fill = rgba_create(rgba); return Undefined(); } @@ -1032,10 +1028,13 @@ Context2d::SetFillColor(const Arguments &args) { Handle Context2d::SetStrokeColor(const Arguments &args) { 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(args.This()); context->state->strokePattern = NULL; - RGBA(context->state->stroke,r,g,b,a); + context->state->stroke = rgba_create(rgba); return Undefined(); } diff --git a/src/color.cc b/src/color.cc index 9a6a038..0a05cbf 100644 --- a/src/color.cc +++ b/src/color.cc @@ -25,7 +25,7 @@ NAME += *str++ - '0'; \ } while (*str >= '0' && *str <= '9'); \ } else { \ - return -1; \ + return 0; \ } \ while (' ' == *str || ',' == *str) str++; @@ -227,10 +227,10 @@ h(char c) { rgba_t rgba_create(uint32_t rgba) { rgba_t color; - color.r = rgba >> 24; - color.g = (rgba & 0x00ff0000) >> 16; - color.b = (rgba & 0x0000ff00) >> 8; - color.a = rgba & 0xff; + color.r = (double) (rgba >> 24) / 255; + color.g = (double) ((rgba & 0x00ff0000) >> 16) / 255; + color.b = (double) ((rgba & 0x0000ff00) >> 8) / 255; + color.a = (double) (rgba & 0xff) / 255; return color; } @@ -297,7 +297,7 @@ rgba_from_rgb_string(const char *str) { CHANNEL(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 -1; + return 0; } /* @@ -346,7 +346,7 @@ rgba_from_hex_string(const char *str) { size_t len = strlen(str); if (6 == len) return rgba_from_hex6_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)) return color.val; } - return -1; + return 0; } /*