Class: Colours::RGB

Inherits:
Base
  • Object
show all
Defined in:
lib/colours/rgb/rgb.rb

Overview

Colours::RGB

Constant Summary collapse

DEFAULT_VALUES =
#

DEFAULT_VALUES

#
[255, 0, 0]
DEFAULT_VALUE =
#

DEFAULT_VALUE

#
0

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#commandline_arguments?, #e, #extend_module_256_colours, #first_argument?, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(original_input = ARGV, run_already = true) ⇒ RGB

#

initialize

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

[106, 90, 205]

In that case, we need to handle this situation differently.

#


46
47
48
49
50
51
52
53
54
55
# File 'lib/colours/rgb/rgb.rb', line 46

def initialize(
    original_input = ARGV,
    run_already    = true
  )
  reset
  set_commandline_arguments(
    original_input
  )
  run if run_already
end

Class Method Details

.[](r = 255, g = 0, b = 0) ⇒ Object

#

Colours::RgbToHex[]

Easier access to the conversion facility.

Usage example:

Colours::RgbToHex[22,33,44] # => "#16212C"
#


227
228
229
230
231
232
233
234
# File 'lib/colours/rgb/rgb.rb', line 227

def self.[](
    r = 255,
    g =   0,
    b =   0
  )
  _ = new(r,g,b)
  return '#%02x%02x%02x'.upcase % _.rgb
end

Instance Method Details

#add(i = 1) ⇒ Object

#

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().

#


201
202
203
204
205
206
207
# File 'lib/colours/rgb/rgb.rb', line 201

def add(i = 1)
  i = i.to_i
  @r += i
  @g += i
  @b += i
  update_hexstring
end

#b?Boolean Also known as: b

#

b?

#

Returns:

  • (Boolean)


149
150
151
# File 'lib/colours/rgb/rgb.rb', line 149

def b?
  @b
end

#check_validity_of(i) ⇒ Object

#

check_validity_of

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

#


131
132
133
134
135
136
# File 'lib/colours/rgb/rgb.rb', line 131

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(_ = commandline_arguments? ) ⇒ Object

#

determine_rgb_values_from_the_commandline_arguments

#


170
171
172
173
174
175
176
177
178
179
# File 'lib/colours/rgb/rgb.rb', line 170

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_conversionObject Also known as: update_hexstring

#

do_the_conversion

#


141
142
143
144
# File 'lib/colours/rgb/rgb.rb', line 141

def do_the_conversion
  @hexstring = '#%02x%02x%02x'.upcase % rgb()
  return @hexstring
end

#g?Boolean Also known as: g

#

g?

#

Returns:

  • (Boolean)


156
157
158
# File 'lib/colours/rgb/rgb.rb', line 156

def g?
  @g
end

#hexstring?Boolean Also known as: hexstring

#

hexstring?

#

Returns:

  • (Boolean)


191
192
193
# File 'lib/colours/rgb/rgb.rb', line 191

def hexstring?
  @hexstring
end

#lighten(i = 35) ⇒ Object

#

lighten

This method will lighten up a colour.

Usage example:

Colours::RgbToHex.new('#ffffff').lighten # => "#232323"
#


121
122
123
# File 'lib/colours/rgb/rgb.rb', line 121

def lighten(i = 35)
  add(i) # Hardcoded to add +35 to each of R, G and B.
end

#r?Boolean Also known as: r

#

r?

#

Returns:

  • (Boolean)


163
164
165
# File 'lib/colours/rgb/rgb.rb', line 163

def r?
  @r
end

#resetObject

#

reset (reset tag)

#


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/colours/rgb/rgb.rb', line 60

def reset
  # ======================================================================= #
  # === @hexstring
  # ======================================================================= #
  @hexstring = nil
  # ======================================================================= #
  # Use default values for RGB - all 0.
  # ======================================================================= #
  @r = 0
  @g = 0
  @b = 0
end

#rgbObject

#

rgb

#


184
185
186
# File 'lib/colours/rgb/rgb.rb', line 184

def rgb
  [ @r, @g, @b ]
end

#runObject

#

run

#


212
213
214
215
# File 'lib/colours/rgb/rgb.rb', line 212

def run
  determine_rgb_values_from_the_commandline_arguments
  do_the_conversion
end

#set_b(i = nil) ⇒ Object

#

set_b

#


104
105
106
107
108
109
# File 'lib/colours/rgb/rgb.rb', line 104

def set_b(i = nil)
  i = DEFAULT_VALUE if i.nil?
  i = i.to_i
  check_validity_of(i)
  @b = i
end

#set_g(i = nil) ⇒ Object

#

set_g

#


94
95
96
97
98
99
# File 'lib/colours/rgb/rgb.rb', line 94

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 = ) ⇒ Object

#

set_r

#


85
86
87
88
89
# File 'lib/colours/rgb/rgb.rb', line 85

def set_r(i = DEFAULT_VALUES[0])
  i = i.to_i
  check_validity_of(i)
  @r = i
end

#set_rgb(r, g, b) ⇒ Object

#

set_rgb

#


76
77
78
79
80
# File 'lib/colours/rgb/rgb.rb', line 76

def set_rgb(r,g,b)
  set_r(r)
  set_g(g)
  set_b(b)
end