UNPKG

3.02 kBtext/x-cView Raw
1// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
2
3#include "CanvasGradient.h"
4
5#include "Canvas.h"
6#include "color.h"
7
8using namespace v8;
9
10Nan::Persistent<FunctionTemplate> Gradient::constructor;
11
12/*
13 * Initialize CanvasGradient.
14 */
15
16void
17Gradient::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
18 Nan::HandleScope scope;
19
20 // Constructor
21 Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(Gradient::New);
22 constructor.Reset(ctor);
23 ctor->InstanceTemplate()->SetInternalFieldCount(1);
24 ctor->SetClassName(Nan::New("CanvasGradient").ToLocalChecked());
25
26 // Prototype
27 Nan::SetPrototypeMethod(ctor, "addColorStop", AddColorStop);
28 Local<Context> ctx = Nan::GetCurrentContext();
29 Nan::Set(target,
30 Nan::New("CanvasGradient").ToLocalChecked(),
31 ctor->GetFunction(ctx).ToLocalChecked());
32}
33
34/*
35 * Initialize a new CanvasGradient.
36 */
37
38NAN_METHOD(Gradient::New) {
39 if (!info.IsConstructCall()) {
40 return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
41 }
42
43 // Linear
44 if (4 == info.Length()) {
45 Gradient *grad = new Gradient(
46 Nan::To<double>(info[0]).FromMaybe(0)
47 , Nan::To<double>(info[1]).FromMaybe(0)
48 , Nan::To<double>(info[2]).FromMaybe(0)
49 , Nan::To<double>(info[3]).FromMaybe(0));
50 grad->Wrap(info.This());
51 info.GetReturnValue().Set(info.This());
52 return;
53 }
54
55 // Radial
56 if (6 == info.Length()) {
57 Gradient *grad = new Gradient(
58 Nan::To<double>(info[0]).FromMaybe(0)
59 , Nan::To<double>(info[1]).FromMaybe(0)
60 , Nan::To<double>(info[2]).FromMaybe(0)
61 , Nan::To<double>(info[3]).FromMaybe(0)
62 , Nan::To<double>(info[4]).FromMaybe(0)
63 , Nan::To<double>(info[5]).FromMaybe(0));
64 grad->Wrap(info.This());
65 info.GetReturnValue().Set(info.This());
66 return;
67 }
68
69 return Nan::ThrowTypeError("invalid arguments");
70}
71
72/*
73 * Add color stop.
74 */
75
76NAN_METHOD(Gradient::AddColorStop) {
77 if (!info[0]->IsNumber())
78 return Nan::ThrowTypeError("offset required");
79 if (!info[1]->IsString())
80 return Nan::ThrowTypeError("color string required");
81
82 Gradient *grad = Nan::ObjectWrap::Unwrap<Gradient>(info.This());
83 short ok;
84 Nan::Utf8String str(info[1]);
85 uint32_t rgba = rgba_from_string(*str, &ok);
86
87 if (ok) {
88 rgba_t color = rgba_create(rgba);
89 cairo_pattern_add_color_stop_rgba(
90 grad->pattern()
91 , Nan::To<double>(info[0]).FromMaybe(0)
92 , color.r
93 , color.g
94 , color.b
95 , color.a);
96 } else {
97 return Nan::ThrowTypeError("parse color failed");
98 }
99}
100
101/*
102 * Initialize linear gradient.
103 */
104
105Gradient::Gradient(double x0, double y0, double x1, double y1) {
106 _pattern = cairo_pattern_create_linear(x0, y0, x1, y1);
107}
108
109/*
110 * Initialize radial gradient.
111 */
112
113Gradient::Gradient(double x0, double y0, double r0, double x1, double y1, double r1) {
114 _pattern = cairo_pattern_create_radial(x0, y0, r0, x1, y1, r1);
115}
116
117/*
118 * Destroy the pattern.
119 */
120
121Gradient::~Gradient() {
122 cairo_pattern_destroy(_pattern);
123}