Initial commit
[yaffs-website] / node_modules / node-sass / src / sass_types / color.cpp
1 #include <nan.h>
2 #include "color.h"
3
4 namespace SassTypes
5 {
6   Color::Color(Sass_Value* v) : SassValueWrapper(v) {}
7
8   Sass_Value* Color::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
9     double a = 1.0, r = 0, g = 0, b = 0;
10     unsigned argb;
11
12     switch (raw_val.size()) {
13     case 1:
14       if (!raw_val[0]->IsNumber()) {
15         return fail("Only argument should be an integer.", out);
16       }
17
18       argb = Nan::To<int32_t>(raw_val[0]).FromJust();
19       a = (double)((argb >> 030) & 0xff) / 0xff;
20       r = (double)((argb >> 020) & 0xff);
21       g = (double)((argb >> 010) & 0xff);
22       b = (double)(argb & 0xff);
23       break;
24
25     case 4:
26       if (!raw_val[3]->IsNumber()) {
27         return fail("Constructor arguments should be numbers exclusively.", out);
28       }
29
30       a = Nan::To<double>(raw_val[3]).FromJust();
31       // fall through vvv
32
33     case 3:
34       if (!raw_val[0]->IsNumber() || !raw_val[1]->IsNumber() || !raw_val[2]->IsNumber()) {
35         return fail("Constructor arguments should be numbers exclusively.", out);
36       }
37
38       r = Nan::To<double>(raw_val[0]).FromJust();
39       g = Nan::To<double>(raw_val[1]).FromJust();
40       b = Nan::To<double>(raw_val[2]).FromJust();
41       break;
42
43     case 0:
44       break;
45
46     default:
47       return fail("Constructor should be invoked with either 0, 1, 3 or 4 arguments.", out);
48     }
49
50     return *out = sass_make_color(r, g, b, a);
51   }
52
53   void Color::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
54     Nan::SetPrototypeMethod(proto, "getR", GetR);
55     Nan::SetPrototypeMethod(proto, "getG", GetG);
56     Nan::SetPrototypeMethod(proto, "getB", GetB);
57     Nan::SetPrototypeMethod(proto, "getA", GetA);
58     Nan::SetPrototypeMethod(proto, "setR", SetR);
59     Nan::SetPrototypeMethod(proto, "setG", SetG);
60     Nan::SetPrototypeMethod(proto, "setB", SetB);
61     Nan::SetPrototypeMethod(proto, "setA", SetA);
62   }
63
64   NAN_METHOD(Color::GetR) {
65     info.GetReturnValue().Set(sass_color_get_r(unwrap(info.This())->value));
66   }
67
68   NAN_METHOD(Color::GetG) {
69     info.GetReturnValue().Set(sass_color_get_g(unwrap(info.This())->value));
70   }
71
72   NAN_METHOD(Color::GetB) {
73     info.GetReturnValue().Set(sass_color_get_b(unwrap(info.This())->value));
74   }
75
76   NAN_METHOD(Color::GetA) {
77     info.GetReturnValue().Set(sass_color_get_a(unwrap(info.This())->value));
78   }
79
80   NAN_METHOD(Color::SetR) {
81     if (info.Length() != 1) {
82       return Nan::ThrowTypeError("Expected just one argument");
83     }
84
85     if (!info[0]->IsNumber()) {
86       return Nan::ThrowTypeError("Supplied value should be a number");
87     }
88
89     sass_color_set_r(unwrap(info.This())->value, Nan::To<double>(info[0]).FromJust());
90   }
91
92   NAN_METHOD(Color::SetG) {
93     if (info.Length() != 1) {
94       return Nan::ThrowTypeError("Expected just one argument");
95     }
96
97     if (!info[0]->IsNumber()) {
98       return Nan::ThrowTypeError("Supplied value should be a number");
99     }
100
101     sass_color_set_g(unwrap(info.This())->value, Nan::To<double>(info[0]).FromJust());
102   }
103
104   NAN_METHOD(Color::SetB) {
105     if (info.Length() != 1) {
106       return Nan::ThrowTypeError("Expected just one argument");
107     }
108
109     if (!info[0]->IsNumber()) {
110       return Nan::ThrowTypeError("Supplied value should be a number");
111     }
112
113     sass_color_set_b(unwrap(info.This())->value, Nan::To<double>(info[0]).FromJust());
114   }
115
116   NAN_METHOD(Color::SetA) {
117     if (info.Length() != 1) {
118       return Nan::ThrowTypeError("Expected just one argument");
119     }
120
121     if (!info[0]->IsNumber()) {
122       return Nan::ThrowTypeError("Supplied value should be a number");
123     }
124
125     sass_color_set_a(unwrap(info.This())->value, Nan::To<double>(info[0]).FromJust());
126   }
127 }