Class: Rubyvis::Color::Rgb

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

Overview

Represents a color in RGB space.

Instance Attribute Summary collapse

Attributes inherited from Rubyvis::Color

#color, #opacity

Instance Method Summary collapse

Methods inherited from Rubyvis::Color

#brighter, names, transparent

Constructor Details

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

Constructs a new RGB color with the specified channel values.



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

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

Instance Attribute Details

#aObject (readonly)

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



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

def a
  @a
end

#bObject (readonly)

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



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

def b
  @b
end

#gObject (readonly)

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



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

def g
  @g
end

#rObject (readonly)

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



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

def r
  @r
end

Instance Method Details

#==(v) ⇒ Object



301
302
303
# File 'lib/rubyvis/color/color.rb', line 301

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.



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

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.



317
318
319
# File 'lib/rubyvis/color/color.rb', line 317

def blue(b1)
  Rubyvis.rgb(r,g,b1,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.



356
357
358
359
360
361
362
363
# File 'lib/rubyvis/color/color.rb', line 356

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.



312
313
314
# File 'lib/rubyvis/color/color.rb', line 312

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

#lighter(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.



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

def lighter(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

#red(r1) ⇒ Object

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



307
308
309
# File 'lib/rubyvis/color/color.rb', line 307

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.



327
328
329
# File 'lib/rubyvis/color/color.rb', line 327

def rgb
  self
end

#to_sObject



365
366
367
# File 'lib/rubyvis/color/color.rb', line 365

def to_s
  @color
end