Welcome to mirror list, hosted at ThFree Co, Russian Federation.

color.cpp « sass_types « src « node-sass « node_modules - github.com/austingebauer/devise.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 40358a2c3b13790b35ceeb1cdda91115d493ce35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <nan.h>
#include "color.h"

namespace SassTypes
{
  Color::Color(Sass_Value* v) : SassValueWrapper(v) {}

  Sass_Value* Color::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
    double a = 1.0, r = 0, g = 0, b = 0;
    unsigned argb;

    switch (raw_val.size()) {
    case 1:
      if (!raw_val[0]->IsNumber()) {
        return fail("Only argument should be an integer.", out);
      }

      argb = Nan::To<int32_t>(raw_val[0]).FromJust();
      a = (double)((argb >> 030) & 0xff) / 0xff;
      r = (double)((argb >> 020) & 0xff);
      g = (double)((argb >> 010) & 0xff);
      b = (double)(argb & 0xff);
      break;

    case 4:
      if (!raw_val[3]->IsNumber()) {
        return fail("Constructor arguments should be numbers exclusively.", out);
      }

      a = Nan::To<double>(raw_val[3]).FromJust();
      NODE_SASS_FALLTHROUGH;

    case 3:
      if (!raw_val[0]->IsNumber() || !raw_val[1]->IsNumber() || !raw_val[2]->IsNumber()) {
        return fail("Constructor arguments should be numbers exclusively.", out);
      }

      r = Nan::To<double>(raw_val[0]).FromJust();
      g = Nan::To<double>(raw_val[1]).FromJust();
      b = Nan::To<double>(raw_val[2]).FromJust();
      break;

    case 0:
      break;

    default:
      return fail("Constructor should be invoked with either 0, 1, 3 or 4 arguments.", out);
    }

    return *out = sass_make_color(r, g, b, a);
  }

  void Color::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
    Nan::SetPrototypeMethod(proto, "getR", GetR);
    Nan::SetPrototypeMethod(proto, "getG", GetG);
    Nan::SetPrototypeMethod(proto, "getB", GetB);
    Nan::SetPrototypeMethod(proto, "getA", GetA);
    Nan::SetPrototypeMethod(proto, "setR", SetR);
    Nan::SetPrototypeMethod(proto, "setG", SetG);
    Nan::SetPrototypeMethod(proto, "setB", SetB);
    Nan::SetPrototypeMethod(proto, "setA", SetA);
  }

  NAN_METHOD(Color::GetR) {
    info.GetReturnValue().Set(sass_color_get_r(Color::Unwrap<Color>(info.This())->value));
  }

  NAN_METHOD(Color::GetG) {
    info.GetReturnValue().Set(sass_color_get_g(Color::Unwrap<Color>(info.This())->value));
  }

  NAN_METHOD(Color::GetB) {
    info.GetReturnValue().Set(sass_color_get_b(Color::Unwrap<Color>(info.This())->value));
  }

  NAN_METHOD(Color::GetA) {
    info.GetReturnValue().Set(sass_color_get_a(Color::Unwrap<Color>(info.This())->value));
  }

  NAN_METHOD(Color::SetR) {
    if (info.Length() != 1) {
      return Nan::ThrowTypeError("Expected just one argument");
    }

    if (!info[0]->IsNumber()) {
      return Nan::ThrowTypeError("Supplied value should be a number");
    }

    sass_color_set_r(Color::Unwrap<Color>(info.This())->value, Nan::To<double>(info[0]).FromJust());
  }

  NAN_METHOD(Color::SetG) {
    if (info.Length() != 1) {
      return Nan::ThrowTypeError("Expected just one argument");
    }

    if (!info[0]->IsNumber()) {
      return Nan::ThrowTypeError("Supplied value should be a number");
    }

    sass_color_set_g(Color::Unwrap<Color>(info.This())->value, Nan::To<double>(info[0]).FromJust());
  }

  NAN_METHOD(Color::SetB) {
    if (info.Length() != 1) {
      return Nan::ThrowTypeError("Expected just one argument");
    }

    if (!info[0]->IsNumber()) {
      return Nan::ThrowTypeError("Supplied value should be a number");
    }

    sass_color_set_b(Color::Unwrap<Color>(info.This())->value, Nan::To<double>(info[0]).FromJust());
  }

  NAN_METHOD(Color::SetA) {
    if (info.Length() != 1) {
      return Nan::ThrowTypeError("Expected just one argument");
    }

    if (!info[0]->IsNumber()) {
      return Nan::ThrowTypeError("Supplied value should be a number");
    }

    sass_color_set_a(Color::Unwrap<Color>(info.This())->value, Nan::To<double>(info[0]).FromJust());
  }
}