Class: Paleta::Color
Overview
Represents a color
Instance Attribute Summary collapse
-
#blue ⇒ Object
Returns the value of attribute blue.
-
#green ⇒ Object
Returns the value of attribute green.
-
#hex ⇒ Object
Returns the value of attribute hex.
-
#hue ⇒ Object
Returns the value of attribute hue.
-
#lightness ⇒ Object
Returns the value of attribute lightness.
-
#red ⇒ Object
Returns the value of attribute red.
-
#saturation ⇒ Object
Returns the value of attribute saturation.
Instance Method Summary collapse
-
#==(color) ⇒ Boolean
Determine the equality of the receiver and another Color.
-
#complement ⇒ Color
Create a new Color that is the complement of the receiver.
-
#complement! ⇒ Color
Turn the receiver into it’s complement.
-
#darken(percentage = 5) ⇒ Color
Create a copy of the receiver and darken it by a percentage.
-
#darken!(percentage = 5) ⇒ Color
Darken the receiver by a percentage.
-
#desaturate ⇒ Color
Create a copy of the receiver and desaturate it.
-
#desaturate! ⇒ Color
Desaturate the receiver.
-
#initialize(*args) ⇒ Color
constructor
Initailize a Color.
-
#invert ⇒ Color
Create a copy of the receiver and invert it.
-
#invert! ⇒ Color
Invert the receiver.
-
#lighten(percentage = 5) ⇒ Color
Create a copy of the receiver and lighten it by a percentage.
-
#lighten!(percentage = 5) ⇒ Color
Lighten the receiver by a percentage.
-
#similarity(color) ⇒ Number
Calculate the similarity between the receiver and another Color.
-
#to_array(color_model = :rgb) ⇒ Array
Return an array representation of a Color instance,.
Methods included from Math
#distance, #multiple_regression
Constructor Details
#initialize ⇒ Color #initialize(color) ⇒ Color #initialize(model, value) ⇒ Color #initialize(model, value, value, value) ⇒ Color #initialize(value, value, value) ⇒ Color
Initailize a Paleta::Color
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/paleta/color.rb', line 38 def initialize(*args) if args.length == 1 && args[0].is_a?(Color) args[0].instance_variables.each do |key| self.send("#{key[1..key.length]}=".to_sym, args[0].send("#{key[1..key.length]}")) end elsif args.length == 2 && args[0] == :hex && args[1].is_a?(String) # example: new(:hex, "336699") hex_init(args[1]) elsif args.length == 3 && args[0].is_a?(Numeric) && args[1].is_a?(Numeric) && args[2].is_a?(Numeric) # example: new(235, 129, 74) rgb_init(args[0], args[1], args[2]) elsif args.length == 4 && [:rgb, :hsl].include?(args[0]) && args[1].is_a?(Numeric) && args[2].is_a?(Numeric) && args[3].is_a?(Numeric) # example: new(:hsl, 320, 96, 74) rgb_init(args[1], args[2], args[3]) if args[0] == :rgb # example: new(:rgb, 235, 129, 74) hsl_init(args[1], args[2], args[3]) if args[0] == :hsl elsif args.length == 0 # example: new() rgb_init(0, 0, 0) else raise(ArgumentError, "Invalid arguments") end end |
Instance Attribute Details
#blue ⇒ Object
Returns the value of attribute blue.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def blue @blue end |
#green ⇒ Object
Returns the value of attribute green.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def green @green end |
#hex ⇒ Object
Returns the value of attribute hex.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def hex @hex end |
#hue ⇒ Object
Returns the value of attribute hue.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def hue @hue end |
#lightness ⇒ Object
Returns the value of attribute lightness.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def lightness @lightness end |
#red ⇒ Object
Returns the value of attribute red.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def red @red end |
#saturation ⇒ Object
Returns the value of attribute saturation.
8 9 10 |
# File 'lib/paleta/color.rb', line 8 def saturation @saturation end |
Instance Method Details
#==(color) ⇒ Boolean
Determine the equality of the receiver and another Paleta::Color
110 111 112 |
# File 'lib/paleta/color.rb', line 110 def ==(color) color.is_a?(Color) ? (self.hex == color.hex) : false end |
#complement ⇒ Color
Create a new Paleta::Color that is the complement of the receiver
192 193 194 195 196 |
# File 'lib/paleta/color.rb', line 192 def complement copy = self.class.new(self) copy.complement! copy end |
#complement! ⇒ Color
Turn the receiver into it’s complement
200 201 202 203 204 205 |
# File 'lib/paleta/color.rb', line 200 def complement! @hue = (@hue + 180) % 360 update_rgb update_hex self end |
#darken(percentage = 5) ⇒ Color
Create a copy of the receiver and darken it by a percentage
137 138 139 140 141 |
# File 'lib/paleta/color.rb', line 137 def darken(percentage = 5) copy = self.dup copy.darken!(percentage) copy end |
#darken!(percentage = 5) ⇒ Color
Darken the receiver by a percentage
146 147 148 149 150 151 152 |
# File 'lib/paleta/color.rb', line 146 def darken!(percentage = 5) @lightness -= percentage @lightness = 0 if @lightness < 0 update_rgb update_hex self end |
#desaturate ⇒ Color
Create a copy of the receiver and desaturate it
175 176 177 178 179 |
# File 'lib/paleta/color.rb', line 175 def desaturate copy = self.class.new(self) copy.desaturate! copy end |
#desaturate! ⇒ Color
Desaturate the receiver
183 184 185 186 187 188 |
# File 'lib/paleta/color.rb', line 183 def desaturate! @saturation = 0 update_rgb update_hex self end |
#invert ⇒ Color
Create a copy of the receiver and invert it
156 157 158 159 160 |
# File 'lib/paleta/color.rb', line 156 def invert copy = self.class.new(self) copy.invert! copy end |
#invert! ⇒ Color
Invert the receiver
164 165 166 167 168 169 170 171 |
# File 'lib/paleta/color.rb', line 164 def invert! @red = 255 - @red @green = 255 - @green @blue = 255 - @blue update_hsl update_hex self end |
#lighten(percentage = 5) ⇒ Color
Create a copy of the receiver and lighten it by a percentage
117 118 119 120 121 |
# File 'lib/paleta/color.rb', line 117 def lighten(percentage = 5) copy = self.dup copy.lighten!(percentage) copy end |
#lighten!(percentage = 5) ⇒ Color
Lighten the receiver by a percentage
126 127 128 129 130 131 132 |
# File 'lib/paleta/color.rb', line 126 def lighten!(percentage = 5) @lightness += percentage @lightness = 100 if @lightness > 100 update_rgb update_hex self end |
#similarity(color) ⇒ Number
Calculate the similarity between the receiver and another Paleta::Color
210 211 212 |
# File 'lib/paleta/color.rb', line 210 def similarity(color) distance({ :r => @red, :g => @green, :b => @blue}, { :r => color.red, :g => color.green, :b => color.blue}) / sqrt(3 * (255 ** 2)) end |
#to_array(color_model = :rgb) ⇒ Array
Return an array representation of a Paleta::Color instance,
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/paleta/color.rb', line 217 def to_array(color_model = :rgb) color_model = color_model.to_sym unless color_model.is_a? Symbol if color_model == :rgb array = [self.red, self.green, self.blue] elsif color_model == :hsl array = [self.hue, self.saturation, self.lightness] else raise(ArgumentError, "Argument must be :rgb or :hsl") end array end |