Class: Colours::RGB
- Inherits:
-
Object
- Object
- Colours::RGB
- 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
-
.[](r = 255, g = 0, b = 0) ⇒ Object
# === RgbToHex[].
Instance Method Summary collapse
-
#add(i = 1) ⇒ Object
# === add.
-
#b? ⇒ Boolean
(also: #b)
# === b? ========================================================================= #.
-
#check_validity_of(i) ⇒ Object
# === check_validity_of.
-
#determine_rgb_values_from_the_commandline_arguments ⇒ Object
# === determine_rgb_values_from_the_commandline_arguments ========================================================================= #.
-
#do_the_conversion ⇒ Object
(also: #update_hexstring)
# === do_the_conversion ========================================================================= #.
-
#g? ⇒ Boolean
(also: #g)
# === g? ========================================================================= #.
-
#hexstring? ⇒ Boolean
(also: #hexstring)
# === hexstring? ========================================================================= #.
-
#initialize(original_input = ARGV, run_already = true) ⇒ RGB
constructor
# === initialize.
-
#lighten ⇒ Object
# === lighten.
-
#r? ⇒ Boolean
(also: #r)
# === r? ========================================================================= #.
-
#reset ⇒ Object
# === reset =========================================================================== #.
-
#rgb ⇒ Object
# === rgb ========================================================================= #.
-
#run ⇒ Object
# === run =========================================================================== #.
-
#set_b(i = nil) ⇒ Object
# === set_b ========================================================================= #.
-
#set_commandline_arguments(i) ⇒ Object
# === set_commandline_arguments =========================================================================== #.
-
#set_g(i = nil) ⇒ Object
# === set_g ========================================================================= #.
-
#set_r(i = DEFAULT_VALUES[0]) ⇒ Object
# === set_r ========================================================================= #.
-
#set_rgb(r, g, b) ⇒ Object
# === set_rgb ========================================================================= #.
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 continue differently.
#
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/colours/rgb/rgb.rb', line 47 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
#
RgbToHex[]
Easier access to the conversion facility.
Usage example:
RgbToHex[22,33,44] # => "#16212C"
#
219 220 221 222 |
# File 'lib/colours/rgb/rgb.rb', line 219 def self.[](r = 255, g = 0, b = 0) _ = RgbToHex.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().
#
195 196 197 198 199 200 201 |
# File 'lib/colours/rgb/rgb.rb', line 195 def add(i = 1) i = i.to_i @r += i @g += i @b += i update_hexstring end |
#b? ⇒ Boolean Also known as: b
#
b?
#
75 76 77 |
# File 'lib/colours/rgb/rgb.rb', line 75 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.
#
126 127 128 129 130 131 |
# File 'lib/colours/rgb/rgb.rb', line 126 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 ⇒ Object
#
determine_rgb_values_from_the_commandline_arguments
#
103 104 105 106 107 108 109 110 111 |
# File 'lib/colours/rgb/rgb.rb', line 103 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 ⇒ Object Also known as: update_hexstring
#
do_the_conversion
#
136 137 138 139 |
# File 'lib/colours/rgb/rgb.rb', line 136 def do_the_conversion @hexstring = '#%02x%02x%02x'.upcase % rgb() return @hexstring end |
#g? ⇒ Boolean Also known as: g
#
g?
#
82 83 84 |
# File 'lib/colours/rgb/rgb.rb', line 82 def g? @g end |
#hexstring? ⇒ Boolean Also known as: hexstring
#
hexstring?
#
89 90 91 |
# File 'lib/colours/rgb/rgb.rb', line 89 def hexstring? @hexstring end |
#lighten ⇒ Object
#
lighten
This lightens up a colour.
RgbToHex.new('#ffffff').lighten # => "#232323"
#
185 186 187 |
# File 'lib/colours/rgb/rgb.rb', line 185 def lighten add(35) # Hardcoded to add +35 to each of R, G and B. end |
#r? ⇒ Boolean Also known as: r
#
r?
#
96 97 98 |
# File 'lib/colours/rgb/rgb.rb', line 96 def r? @r end |
#reset ⇒ Object
#
reset
#
68 69 70 |
# File 'lib/colours/rgb/rgb.rb', line 68 def reset @r, @g, @b = 0, 0, 0 # Use default values for RGB. end |
#rgb ⇒ Object
#
rgb
#
116 117 118 |
# File 'lib/colours/rgb/rgb.rb', line 116 def rgb [ @r, @g, @b ] end |
#run ⇒ Object
#
run
#
206 207 208 209 |
# File 'lib/colours/rgb/rgb.rb', line 206 def run determine_rgb_values_from_the_commandline_arguments do_the_conversion end |
#set_b(i = nil) ⇒ Object
#
set_b
#
172 173 174 175 176 177 |
# File 'lib/colours/rgb/rgb.rb', line 172 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) ⇒ Object
#
set_commandline_arguments
#
61 62 63 |
# File 'lib/colours/rgb/rgb.rb', line 61 def set_commandline_arguments(i) @commandline_arguments = [i].flatten.compact end |
#set_g(i = nil) ⇒ Object
#
set_g
#
162 163 164 165 166 167 |
# File 'lib/colours/rgb/rgb.rb', line 162 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]) ⇒ Object
#
set_r
#
153 154 155 156 157 |
# File 'lib/colours/rgb/rgb.rb', line 153 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
#
144 145 146 147 148 |
# File 'lib/colours/rgb/rgb.rb', line 144 def set_rgb(r,g,b) set_r(r) set_g(g) set_b(b) end |