Class: Rubyvis::Color::Rgb

Inherits:
Rubyvis::Color show all
Defined in:
lib/rubyvis/color/color.rb

Overview

Represents a color in RGB space.

Constant Summary

Constants inherited from Rubyvis::Color

NAMES

Instance Attribute Summary collapse

Attributes inherited from Rubyvis::Color

#color, #opacity

Instance Method Summary collapse

Methods inherited from Rubyvis::Color

transparent

Constructor Details

#initialize(r, g, b, a) ⇒ Rgb

Constructs a new RGB color with the specified channel values.



290
291
292
293
294
295
296
297
# File 'lib/rubyvis/color/color.rb', line 290

def initialize(r,g,b,a)
  @r=r
  @b=b
  @g=g
  @a=a
  @opacity=a
  @color= @a > 0 ? "rgb(#{r.to_i},#{g.to_i},#{b.to_i})" : "none"
end

Instance Attribute Details

#aObject (readonly)

The alpha channel, a float in [0, 1].



288
289
290
# File 'lib/rubyvis/color/color.rb', line 288

def a
  @a
end

#bObject (readonly)

The blue channel, an integer in [0, 255].



284
285
286
# File 'lib/rubyvis/color/color.rb', line 284

def b
  @b
end

#gObject (readonly)

The green channel, an integer in [0, 255].



286
287
288
# File 'lib/rubyvis/color/color.rb', line 286

def g
  @g
end

#rObject (readonly)

The red channel, an integer in [0, 255].



282
283
284
# File 'lib/rubyvis/color/color.rb', line 282

def r
  @r
end

Instance Method Details

#==(v) ⇒ Object



298
299
300
# File 'lib/rubyvis/color/color.rb', line 298

def ==(v)
  self.class==v.class and @r==v.r and @b==v.b and @g==v.g and @a==v.a
end

#alpha(a1) ⇒ Object

Constructs a new RGB color with the same red, green and blue channels as this color, with the specified alpha channel.



320
321
322
# File 'lib/rubyvis/color/color.rb', line 320

def alpha(a1)
  Rubyvis.rgb(r,g,b,a1)
end

#blue(b1) ⇒ Object

Constructs a new RGB color with the same red, green and alpha channels as this color, with the specified blue channel.



314
315
316
# File 'lib/rubyvis/color/color.rb', line 314

def blue(b1)
  Rubyvis.rgb(r,g,b1,a)
end

#brighter(k = 1) ⇒ Object

Returns a new color that is a brighter version of this color. This method applies an arbitrary scale factor to each of the three RGB components of this color to create a brighter version of this color. Although brighter and darker are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rubyvis/color/color.rb', line 332

def brighter(k=1)
  k = 0.7**k
  i = 30
  r=self.r
  g=self.g
  b=self.b
  return Rubyvis.rgb(i, i, i, a) if (!r and !g and !b) 
  r = i if (r and (r < i)) 
  g = i if (g and (g < i)) 
  b = i if (b and (b < i))
  Rubyvis.rgb(
    [255, (r/k).floor].min,
    [255, (g/k).floor].min,
    [255, (b/k).floor].min,
  a)
end

#darker(k = 1) ⇒ Object

Returns a new color that is a darker version of this color. This method applies an arbitrary scale factor to each of the three RGB components of this color to create a darker version of this color. Although brighter and darker are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors.



353
354
355
356
357
358
359
360
# File 'lib/rubyvis/color/color.rb', line 353

def darker(k=1)
  k = 0.7 ** k
  Rubyvis.rgb(
    [0, (k * r).floor].max,
    [0, (k * g).floor].max,
    [0, (k * b).floor].max,
    a)
end

#green(g1) ⇒ Object

Constructs a new RGB color with the same red, blue and alpha channels as this color, with the specified green channel.



309
310
311
# File 'lib/rubyvis/color/color.rb', line 309

def green(g1)
  Rubyvis.rgb(r,g1,b,a)
end

#red(r1) ⇒ Object

Constructs a new RGB color with the same green, blue and alpha channels as this color, with the specified red channel.



304
305
306
# File 'lib/rubyvis/color/color.rb', line 304

def red(r1)
  Rubyvis.rgb(r1,g,b,a)
end

#rgbObject

Returns the RGB color equivalent to this color. This method is abstract and must be implemented by subclasses.



324
325
326
# File 'lib/rubyvis/color/color.rb', line 324

def rgb
  self
end

#to_sObject



362
363
364
# File 'lib/rubyvis/color/color.rb', line 362

def to_s
  @color
end