Class: Paleta::Color

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/paleta/color.rb

Overview

Represents a color

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Math

#distance, #multiple_regression

Constructor Details

#initializeColor #initialize(color) ⇒ Color #initialize(model, value) ⇒ Color #initialize(model, value, value, value) ⇒ Color #initialize(value, value, value) ⇒ Color

Initailize a Paleta::Color

Overloads:

  • #initializeColor

    Initialize a Paleta::Color to black

  • #initialize(color) ⇒ Color

    Initialize a Paleta::Color from a Paleta::Color

    Parameters:

    • color (Color)

      a color to copy

  • #initialize(model, value) ⇒ Color

    Initialize a Paleta::Color with a hex value

    Parameters:

    • model (Symbol)

      the color model, should be :hex in this case

    • value (String)

      a 6 character hexadecimal string

  • #initialize(model, value, value, value) ⇒ Color

    Initialize a Paleta::Color with HSL or RGB component values

    Parameters:

    • model (Symbol)

      the color model, should be :hsl or :rgb

    • (red,hue) (Number)

      the red or hue component value, depending on the value of model

    • (green,saturation) (Number)

      the green or saturation component value

    • (blue,lightness) (Number)

      the blue or lightness component value

  • #initialize(value, value, value) ⇒ Color

    Initialize a Paleta::Color with RGB component values

    Parameters:

    • red (Number)

      the red component value

    • green (Number)

      the green component value

    • blue (Number)

      the blue component value



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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")
    # example: new(:hex, "#336699")
    # example: new(:hex, "#fff")
    color_hex = args[1]
    color_hex = color_hex[1..-1] if color_hex.start_with?('#')

    # These are all string operations to make a "fea" into a "ffeeaa"
    color_hex = color_hex[0] * 2 + color_hex[1] * 2 + color_hex[2] * 2 if color_hex.length == 3

    hex_init(color_hex)
  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

#blueObject

Returns the value of attribute blue.



8
9
10
# File 'lib/paleta/color.rb', line 8

def blue
  @blue
end

#greenObject

Returns the value of attribute green.



8
9
10
# File 'lib/paleta/color.rb', line 8

def green
  @green
end

#hexObject

Returns the value of attribute hex.



8
9
10
# File 'lib/paleta/color.rb', line 8

def hex
  @hex
end

#hueObject

Returns the value of attribute hue.



8
9
10
# File 'lib/paleta/color.rb', line 8

def hue
  @hue
end

#lightnessObject

Returns the value of attribute lightness.



8
9
10
# File 'lib/paleta/color.rb', line 8

def lightness
  @lightness
end

#redObject

Returns the value of attribute red.



8
9
10
# File 'lib/paleta/color.rb', line 8

def red
  @red
end

#saturationObject

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

Parameters:

  • color (Color)

    color to compare

Returns:

  • (Boolean)


118
119
120
# File 'lib/paleta/color.rb', line 118

def ==(color)
  color.is_a?(Color) ? (self.hex == color.hex) : false
end

#complementColor

Create a new Paleta::Color that is the complement of the receiver

Returns:

  • (Color)

    a desaturated copy of the receiver



200
201
202
203
204
# File 'lib/paleta/color.rb', line 200

def complement
  copy = self.class.new(self)
  copy.complement!
  copy
end

#complement!Color

Turn the receiver into it’s complement

Returns:



208
209
210
211
212
213
# File 'lib/paleta/color.rb', line 208

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

Parameters:

  • percentage (Number) (defaults to: 5)

    percentage by which to darken the Paleta::Color

Returns:

  • (Color)

    a darkened copy of the receiver



145
146
147
148
149
# File 'lib/paleta/color.rb', line 145

def darken(percentage = 5)
  copy = self.dup
  copy.darken!(percentage)
  copy
end

#darken!(percentage = 5) ⇒ Color

Darken the receiver by a percentage

Parameters:

  • percentage (Number) (defaults to: 5)

    percentage by which to darken the Paleta::Color

Returns:



154
155
156
157
158
159
160
# File 'lib/paleta/color.rb', line 154

def darken!(percentage = 5)
  @lightness -= percentage
  @lightness = 0 if @lightness < 0
  update_rgb
  update_hex
  self
end

#desaturateColor

Create a copy of the receiver and desaturate it

Returns:

  • (Color)

    a desaturated copy of the receiver



183
184
185
186
187
# File 'lib/paleta/color.rb', line 183

def desaturate
  copy = self.class.new(self)
  copy.desaturate!
  copy
end

#desaturate!Color

Desaturate the receiver

Returns:



191
192
193
194
195
196
# File 'lib/paleta/color.rb', line 191

def desaturate!
  @saturation = 0
  update_rgb
  update_hex
  self
end

#invertColor

Create a copy of the receiver and invert it

Returns:

  • (Color)

    an inverted copy of the receiver



164
165
166
167
168
# File 'lib/paleta/color.rb', line 164

def invert
  copy = self.class.new(self)
  copy.invert!
  copy
end

#invert!Color

Invert the receiver

Returns:



172
173
174
175
176
177
178
179
# File 'lib/paleta/color.rb', line 172

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

Parameters:

  • percentage (Number) (defaults to: 5)

    percentage by which to lighten the Paleta::Color

Returns:

  • (Color)

    a lightened copy of the receiver



125
126
127
128
129
# File 'lib/paleta/color.rb', line 125

def lighten(percentage = 5)
  copy = self.dup
  copy.lighten!(percentage)
  copy
end

#lighten!(percentage = 5) ⇒ Color

Lighten the receiver by a percentage

Parameters:

  • percentage (Number) (defaults to: 5)

    percentage by which to lighten the Paleta::Color

Returns:



134
135
136
137
138
139
140
# File 'lib/paleta/color.rb', line 134

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

Parameters:

  • color (Color)

    color to calculate the similarity to

Returns:

  • (Number)

    a value in [0..1] with 0 being identical and 1 being as dissimilar as possible



218
219
220
# File 'lib/paleta/color.rb', line 218

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,

Parameters:

  • model (Symbol)

    the color model, should be :rgb or :hsl

Returns:

  • (Array)

    an array of component values



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/paleta/color.rb', line 225

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