class Colours::RGB

Constants

DEFAULT_VALUE
#

DEFAULT_VALUE

#
DEFAULT_VALUES
#

DEFAULT_VALUES

#

Public Class Methods

[](r = 255, g = 0, b = 0) click to toggle source
#

Colours::RgbToHex[]

Easier access to the conversion facility.

Usage example:

Colours::RgbToHex[22,33,44] # => "#16212C"
#
# File lib/colours/rgb/rgb.rb, line 219
def self.[](r = 255, g = 0, b = 0)
  _ = new(r,g,b)
  return '#%02x%02x%02x'.upcase % _.rgb
end
new( original_input = ARGV, run_already = true ) click to toggle source
#

initialize

Keep in mind that the first argument could be something like:

[106, 90, 205]

In that case, we need to continue differently.

#
# File lib/colours/rgb/rgb.rb, line 45
def initialize(
    original_input = ARGV,
    run_already    = true
  )
  reset
  set_commandline_arguments(
    original_input
  )
  run if run_already
end

Public Instance Methods

add(i = 1) click to toggle source
#

add

This method can be used to lighten a R,G,B colour in general. We also use another method for this, though, called .lighten().

#
# File lib/colours/rgb/rgb.rb, line 193
def add(i = 1)
  i = i.to_i
  @r += i
  @g += i
  @b += i
  update_hexstring
end
b()
Alias for: b?
b?() click to toggle source
#

b?

#
# File lib/colours/rgb/rgb.rb, line 73
def b?
  @b
end
Also aliased as: b
check_validity_of(i) click to toggle source
#

check_validity_of

We check if the input is higher than 255, in which case we give back an ArgumentError.

#
# File lib/colours/rgb/rgb.rb, line 124
def check_validity_of(i)
  if i.to_i > 255
    raise ArgumentError,
      'Error: RGB values can not be higher than 255.' 
  end
end
determine_rgb_values_from_the_commandline_arguments() click to toggle source
#

determine_rgb_values_from_the_commandline_arguments

#
# File lib/colours/rgb/rgb.rb, line 101
def determine_rgb_values_from_the_commandline_arguments
  _ = @commandline_arguments
  first = _.first
  if first.is_? Array
    set_r(first[0])
    set_g(first[1])
    set_b(first[2])
  end
end
do_the_conversion() click to toggle source
#

do_the_conversion

#
# File lib/colours/rgb/rgb.rb, line 134
def do_the_conversion
  @hexstring = '#%02x%02x%02x'.upcase % rgb()
  return @hexstring
end
Also aliased as: update_hexstring
g()
Alias for: g?
g?() click to toggle source
#

g?

#
# File lib/colours/rgb/rgb.rb, line 80
def g?
  @g
end
Also aliased as: g
hexstring()
Alias for: hexstring?
hexstring?() click to toggle source
#

hexstring?

#
# File lib/colours/rgb/rgb.rb, line 87
def hexstring?
  @hexstring
end
Also aliased as: hexstring
lighten() click to toggle source
#

lighten

This lightens up a colour.

RgbToHex.new('#ffffff').lighten # => "#232323"
#
# File lib/colours/rgb/rgb.rb, line 183
def lighten
  add(35) # Hardcoded to add +35 to each of R, G and B.
end
r()
Alias for: r?
r?() click to toggle source
#

r?

#
# File lib/colours/rgb/rgb.rb, line 94
def r?
  @r
end
Also aliased as: r
reset() click to toggle source
#

reset

#
# File lib/colours/rgb/rgb.rb, line 66
def reset
  @r, @g, @b = 0, 0, 0 # Use default values for RGB.
end
rgb() click to toggle source
#

rgb

#
# File lib/colours/rgb/rgb.rb, line 114
def rgb
  [ @r, @g, @b ]
end
run() click to toggle source
#

run

#
# File lib/colours/rgb/rgb.rb, line 204
def run
  determine_rgb_values_from_the_commandline_arguments
  do_the_conversion
end
set_b(i = nil) click to toggle source
#

set_b

#
# File lib/colours/rgb/rgb.rb, line 170
def set_b(i = nil)
  i = DEFAULT_VALUE if i.nil?
  i = i.to_i
  check_validity_of(i)
  @b = i
end
set_commandline_arguments(i) click to toggle source
#

set_commandline_arguments

#
# File lib/colours/rgb/rgb.rb, line 59
def set_commandline_arguments(i)
  @commandline_arguments = [i].flatten.compact
end
set_g(i = nil) click to toggle source
#

set_g

#
# File lib/colours/rgb/rgb.rb, line 160
def set_g(i = nil)
  i = DEFAULT_VALUE if i.nil?
  i = i.to_i
  check_validity_of(i)
  @g = i
end
set_r(i = DEFAULT_VALUES[0]) click to toggle source
#

set_r

#
# File lib/colours/rgb/rgb.rb, line 151
def set_r(i = DEFAULT_VALUES[0])
  i = i.to_i
  check_validity_of(i)
  @r = i
end
set_rgb(r,g,b) click to toggle source
#

set_rgb

#
# File lib/colours/rgb/rgb.rb, line 142
def set_rgb(r,g,b)
  set_r(r)
  set_g(g)
  set_b(b)
end
update_hexstring()
Alias for: do_the_conversion