Class: Ray::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/ray/color.rb,
ext/color.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue, alpha = 255) ⇒ Object

Creates a new color. All the parameters must be integers between

0 and 255. Alpha is the transparency (255: opaque, 0: invisible).

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'ext/color.c', line 36

static
VALUE ray_color_init(int argc, VALUE *argv, VALUE self) {
  VALUE r, g, b, a;
  rb_scan_args(argc, argv, "31", &r, &g, &b, &a);
  if (a == Qnil) a = INT2FIX(255);

  say_color *ret = NULL;
  Data_Get_Struct(self, say_color, ret);

  ret->r = ray_color_clamp(NUM2INT(r));
  ret->g = ray_color_clamp(NUM2INT(g));
  ret->b = ray_color_clamp(NUM2INT(b));
  ret->a = ray_color_clamp(NUM2INT(a));

  return Qnil;
}

Class Method Details

.blackObject


7
# File 'lib/ray/color.rb', line 7

def black;   new(0, 0,0);        end

.blueObject


6
# File 'lib/ray/color.rb', line 6

def blue;    new(0, 0, 255);     end

.cyanObject


11
# File 'lib/ray/color.rb', line 11

def cyan;    new(0, 255, 255);   end

.from_hsv(hue, sat, val) ⇒ Ray::Color

Returns Color created from those parameters.

Parameters:

  • hue (Float)

    Hue, between 0 and 360

  • sat (Float)

    Saturation, between 0 and 1

  • val (Float)

    Value, between 0 and 1

Returns:

  • (Ray::Color)

    Color created from those parameters


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ray/color.rb', line 20

def from_hsv(hue, sat, val)
  hue, sat, val = hue.to_f, sat.to_f, val.to_f

  if sat == 0
    new(val * 255, val * 255, val * 255)
  else
    i = (hue / 60).floor % 6
    f = (hue / 60) - i
    l = val * (1 - sat)
    m = val * (1 - f * sat)
    n = val * (1 - (1 - f) * sat)

    case i
    when 0 then new(val * 255, n * 255, l * 255)
    when 1 then new(m * 255, val * 255, l * 255)
    when 2 then new(l * 255, val * 255, n * 255)
    when 3 then new(l * 255, m * 255, val * 255)
    when 4 then new(n * 255, l * 255, val * 255)
    when 5 then new(val * 255, l * 255, m * 255)
    end
  end
end

.fuschiaObject


13
# File 'lib/ray/color.rb', line 13

def fuschia; new(255, 0, 255);   end

.grayObject


10
# File 'lib/ray/color.rb', line 10

def gray;    new(128, 128, 128); end

.greenObject


5
# File 'lib/ray/color.rb', line 5

def green;   new(0, 255, 0);     end

.noneObject


9
# File 'lib/ray/color.rb', line 9

def none;    new(0, 0, 0, 0);    end

.redObject


4
# File 'lib/ray/color.rb', line 4

def red;     new(255, 0, 0);     end

.whiteObject


8
# File 'lib/ray/color.rb', line 8

def white;   new(255, 255, 255); end

.yellowObject


12
# File 'lib/ray/color.rb', line 12

def yellow;  new(255, 255, 0);   end

Instance Method Details

#*(color) ⇒ Ray::Color

Returns Product of two colors, multiplying component pairs before dividing everything by 255.

Parameters:

Returns:

  • (Ray::Color)

    Product of two colors, multiplying component pairs before dividing everything by 255.


83
84
85
86
87
88
# File 'lib/ray/color.rb', line 83

def *(color)
  Ray::Color.new(r * color.r / 255.0,
                 g * color.g / 255.0,
                 b * color.b / 255.0,
                 a * color.a / 255.0)
end

#+(color) ⇒ Ray::Color

Returns Sum of two colors, adding component pairs.

Parameters:

Returns:

  • (Ray::Color)

    Sum of two colors, adding component pairs.


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ray/color.rb', line 68

def +(color)
  r = red   + color.red
  g = green + color.green
  b = blue  + color.blue
  a = alpha + color.alpha

  Ray::Color.new(r > 255 ? 255 : r,
                 g > 255 ? 255 : g,
                 b > 255 ? 255 : b,
                 a > 255 ? 255 : a)
end

#==(obj) ⇒ Object


90
91
92
93
# File 'lib/ray/color.rb', line 90

def ==(obj)
  return false unless obj.is_a? Color
  r == obj.r && g == obj.g && b == obj.b && a == obj.a
end

#aInteger Also known as: alpha

Returns Alpha opacity.

Returns:

  • (Integer)

    Alpha opacity.


92
93
94
95
96
97
98
# File 'ext/color.c', line 92

static
VALUE ray_color_a(VALUE self) {
  say_color *ret;
  Data_Get_Struct(self, say_color, ret);

  return INT2FIX(ret->a);
}

#a=(val) ⇒ Object Also known as: alpha=

Sets the alpha opacity

Parameters:

  • The (Integer)

    new alpha opacity.


153
154
155
156
157
158
159
160
161
162
# File 'ext/color.c', line 153

static
VALUE ray_color_set_a(VALUE self, VALUE val) {
  rb_check_frozen(self);

  say_color *ret;
  Data_Get_Struct(self, say_color, ret);

  ret->a = ray_color_clamp(NUM2INT(val));
  return val;
}

#bInteger Also known as: blue

Returns Blue intensity.

Returns:

  • (Integer)

    Blue intensity.


83
84
85
86
87
88
89
# File 'ext/color.c', line 83

static
VALUE ray_color_b(VALUE self) {
  say_color *ret;
  Data_Get_Struct(self, say_color, ret);

  return INT2FIX(ret->b);
}

#blue=(val) ⇒ Object Also known as: blue=

Sets the blue intensity

Parameters:

  • The (Integer)

    new blue intensity.


137
138
139
140
141
142
143
144
145
146
# File 'ext/color.c', line 137

static
VALUE ray_color_set_b(VALUE self, VALUE val) {
  rb_check_frozen(self);

  say_color *ret;
  Data_Get_Struct(self, say_color, ret);

  ret->b = ray_color_clamp(NUM2INT(val));
  return val;
}

#eql?(obj) ⇒ Boolean

Returns:

  • (Boolean)

95
96
97
# File 'lib/ray/color.rb', line 95

def eql?(obj)
  self.class == obj.class && self == obj
end

#gInteger Also known as: green

Returns Green intensity.

Returns:

  • (Integer)

    Green intensity.


74
75
76
77
78
79
80
# File 'ext/color.c', line 74

static
VALUE ray_color_g(VALUE self) {
  say_color *ret;
  Data_Get_Struct(self, say_color, ret);

  return INT2FIX(ret->g);
}

#g=(val) ⇒ Object Also known as: green=

Sets the green intensity

Parameters:

  • The (Integer)

    new green intensity.


121
122
123
124
125
126
127
128
129
130
# File 'ext/color.c', line 121

static
VALUE ray_color_set_g(VALUE self, VALUE val) {
  rb_check_frozen(self);

  say_color *ret;
  Data_Get_Struct(self, say_color, ret);

  ret->g = ray_color_clamp(NUM2INT(val));
  return val;
}

#hashObject


99
100
101
# File 'lib/ray/color.rb', line 99

def hash
  [r, g, b, a].hash
end

#initialize_copy(other) ⇒ Object


53
54
55
56
57
58
59
60
61
62
# File 'ext/color.c', line 53

static
VALUE ray_color_init_copy(VALUE self, VALUE other) {
  say_color *color = NULL, *source = NULL;
  Data_Get_Struct(self,  say_color, color);
  Data_Get_Struct(other, say_color, source);

  *color = *source;

  return self;
}

#pretty_print(q) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ray/color.rb', line 52

def pretty_print(q)
  q.text("RGB#{'A' if a != 255}(")
  q.pp r
  q.text ", "
  q.pp g
  q.text ", "
  q.pp b
  if a != 255
    q.text ", "
    q.pp a
  end
  q.text ")"
end

#rInteger Also known as: red

Returns Red intensity.

Returns:

  • (Integer)

    Red intensity.


65
66
67
68
69
70
71
# File 'ext/color.c', line 65

static
VALUE ray_color_r(VALUE self) {
  say_color *ret;
  Data_Get_Struct(self, say_color, ret);

  return INT2FIX(ret->r);
}

#r=(val) ⇒ Object Also known as: red=

Sets the red intensity

Parameters:

  • The (Integer)

    new red intensity.


105
106
107
108
109
110
111
112
113
114
# File 'ext/color.c', line 105

static
VALUE ray_color_set_r(VALUE self, VALUE val) {
  rb_check_frozen(self);

  say_color *ret;
  Data_Get_Struct(self, say_color, ret);

  ret->r = ray_color_clamp(NUM2INT(val));
  return val;
}

#to_aObject


103
104
105
# File 'lib/ray/color.rb', line 103

def to_a
  [r, g, b, a]
end

#to_colorObject


44
45
46
# File 'lib/ray/color.rb', line 44

def to_color
  self
end

#to_sObject


48
49
50
# File 'lib/ray/color.rb', line 48

def to_s
  "RGBA(#{r}, #{g}, #{b}, #{a})"
end