Class: Ray::Color
- Inherits:
-
Object
- Object
- Ray::Color
- Defined in:
- lib/ray/color.rb,
ext/color.c
Class Method Summary collapse
- .black ⇒ Object
- .blue ⇒ Object
- .cyan ⇒ Object
-
.from_hsv(hue, sat, val) ⇒ Ray::Color
Color created from those parameters.
- .fuschia ⇒ Object
- .gray ⇒ Object
- .green ⇒ Object
- .none ⇒ Object
- .red ⇒ Object
- .white ⇒ Object
- .yellow ⇒ Object
Instance Method Summary collapse
-
#*(color) ⇒ Ray::Color
Product of two colors, multiplying component pairs before dividing everything by 255.
-
#+(color) ⇒ Ray::Color
Sum of two colors, adding component pairs.
- #==(obj) ⇒ Object
-
#a ⇒ Integer
(also: #alpha)
Alpha opacity.
-
#a=(val) ⇒ Object
(also: #alpha=)
Sets the alpha opacity.
-
#b ⇒ Integer
(also: #blue)
Blue intensity.
-
#blue=(val) ⇒ Object
(also: #blue=)
Sets the blue intensity.
- #eql?(obj) ⇒ Boolean
-
#g ⇒ Integer
(also: #green)
Green intensity.
-
#g=(val) ⇒ Object
(also: #green=)
Sets the green intensity.
- #hash ⇒ Object
-
#initialize(red, green, blue, alpha = 255) ⇒ Object
constructor
Creates a new color.
- #initialize_copy(other) ⇒ Object
- #pretty_print(q) ⇒ Object
-
#r ⇒ Integer
(also: #red)
Red intensity.
-
#r=(val) ⇒ Object
(also: #red=)
Sets the red intensity.
- #to_a ⇒ Object
- #to_color ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(red, green, blue, alpha = 255) ⇒ Object
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
.black ⇒ Object
7 |
# File 'lib/ray/color.rb', line 7 def black; new(0, 0,0); end |
.blue ⇒ Object
6 |
# File 'lib/ray/color.rb', line 6 def blue; new(0, 0, 255); end |
.cyan ⇒ Object
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.
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 |
.fuschia ⇒ Object
13 |
# File 'lib/ray/color.rb', line 13 def fuschia; new(255, 0, 255); end |
.gray ⇒ Object
10 |
# File 'lib/ray/color.rb', line 10 def gray; new(128, 128, 128); end |
.green ⇒ Object
5 |
# File 'lib/ray/color.rb', line 5 def green; new(0, 255, 0); end |
.none ⇒ Object
9 |
# File 'lib/ray/color.rb', line 9 def none; new(0, 0, 0, 0); end |
.red ⇒ Object
4 |
# File 'lib/ray/color.rb', line 4 def red; new(255, 0, 0); end |
.white ⇒ Object
8 |
# File 'lib/ray/color.rb', line 8 def white; new(255, 255, 255); end |
.yellow ⇒ Object
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.
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.
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 |
#a ⇒ Integer Also known as: alpha
Returns 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=
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;
}
|
#b ⇒ Integer Also known as: blue
Returns 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=
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
95 96 97 |
# File 'lib/ray/color.rb', line 95 def eql?(obj) self.class == obj.class && self == obj end |
#g ⇒ Integer Also known as: green
Returns 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=
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;
}
|
#hash ⇒ Object
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 |
#r ⇒ Integer Also known as: red
Returns 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=
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_a ⇒ Object
103 104 105 |
# File 'lib/ray/color.rb', line 103 def to_a [r, g, b, a] end |
#to_color ⇒ Object
44 45 46 |
# File 'lib/ray/color.rb', line 44 def to_color self end |
#to_s ⇒ Object
48 49 50 |
# File 'lib/ray/color.rb', line 48 def to_s "RGBA(#{r}, #{g}, #{b}, #{a})" end |