Class: Paleta::Palette
- Inherits:
-
Object
- Object
- Paleta::Palette
- Includes:
- Enumerable, Math
- Defined in:
- lib/paleta/palette.rb
Overview
Represents a palette, a collection of Colors
Instance Attribute Summary collapse
-
#colors ⇒ Object
Returns the value of attribute colors.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(obj) ⇒ Palette
-
#[](index) ⇒ Object
Access a Color in the receiver by index.
-
#darken!(percentage = 5) ⇒ Palette
Lighen each Color in the receiver by a percentage.
-
#delete_at(index = 0) ⇒ Object
Remove a Color from the receiver by index.
- #each ⇒ Object
- #fit ⇒ Object
-
#include?(color) ⇒ Boolean
Test if a Color exists in the receiver.
- #initialize(*args) ⇒ Palette constructor
-
#invert! ⇒ Palette
Invert each Color in the receiver by a percentage.
-
#lighten!(percentage = 5) ⇒ Palette
Lighen each Color in the receiver by a percentage.
-
#pop ⇒ Object
Remove the most recently added Color from the receiver.
- #push(obj) ⇒ Palette
-
#similarity(palette) ⇒ Number
Calculate the similarity between the receiver and another Palette.
- #size ⇒ Number
-
#sort(&blk) ⇒ Palette
Create a new instance of Palette that is a sorted copy of the receiver.
-
#sort!(&blk) ⇒ Object
Sort the Colors in the receiver return [Palette] self.
-
#to_array(color_model = :rgb) ⇒ Array
Return an array representation of a Palette instance,.
Methods included from Math
#distance, #multiple_regression
Constructor Details
#initialize(*args) ⇒ Palette
Initialize a Paleta::Palette from a list of Colors
14 15 16 17 18 |
# File 'lib/paleta/palette.rb', line 14 def initialize(*args) @colors = [] colors = (args.length == 1 && args[0].is_a?(Array)) ? args[0] : args colors.each { |color| self << color } end |
Instance Attribute Details
#colors ⇒ Object
Returns the value of attribute colors.
9 10 11 |
# File 'lib/paleta/palette.rb', line 9 def colors @colors end |
Class Method Details
.generate(opts = {}) ⇒ Palette
Generate a Paleta::Palette from a seed Color
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/paleta/palette.rb', line 155 def self.generate(opts = {}) size = opts[:size] || 5 if !opts[:type].nil? && opts[:type].to_sym == :random return self.generate_random_from_color(opts[:color], size) end unless (opts[:from].to_sym == :color && !opts[:color].nil?) || (opts[:from].to_sym == :image && !opts[:image].nil?) return raise(ArgumentError, 'You must pass :from and it must be either :color or :image, then you must pass :image => "/path/to/img" or :color => color') end if opts[:from].to_sym == :image path = opts[:image] return self.generate_from_image(path, size) end color = opts[:color] type = opts[:type] || :shades case type when :analogous; self.generate_analogous_from_color(color, size) when :complementary; self.generate_complementary_from_color(color, size) when :monochromatic; self.generate_monochromatic_from_color(color, size) when :shades; self.generate_shades_from_color(color, size) when :split_complement; self.generate_split_complement_from_color(color, size) when :tetrad; self.generate_tetrad_from_color(color, size) when :triad; self.generate_triad_from_color(color, size) else raise(ArgumentError, "Palette type is not defined. Try :analogous, :monochromatic, :shades, or :random") end end |
Instance Method Details
#<<(color) ⇒ Palette #<<(palette) ⇒ Palette
Add a Color to the Paleta::Palette
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/paleta/palette.rb', line 27 def <<(obj) if obj.is_a?(Color) @colors << obj elsif obj.is_a?(Palette) @colors |= obj.colors else raise(ArgumentError, "Passed argument is not a Color") end self end |
#[](index) ⇒ Object
Access a Color in the receiver by index
62 63 64 |
# File 'lib/paleta/palette.rb', line 62 def [](index) @colors[index] end |
#darken!(percentage = 5) ⇒ Palette
Lighen each Color in the receiver by a percentage
109 110 111 112 |
# File 'lib/paleta/palette.rb', line 109 def darken!(percentage = 5) @colors.each { |color| color.darken!(percentage) } self end |
#delete_at(index = 0) ⇒ Object
Remove a Color from the receiver by index
56 57 58 |
# File 'lib/paleta/palette.rb', line 56 def delete_at(index = 0) @colors.delete_at(index) end |
#each ⇒ Object
Iterate through each Color in the Paleta::Palette
73 74 75 |
# File 'lib/paleta/palette.rb', line 73 def each @colors.each { |c| yield c } end |
#fit ⇒ Object
202 203 204 205 206 207 208 |
# File 'lib/paleta/palette.rb', line 202 def fit # create a 3xn matrix where n = @colors.size to represent the set of colors reds = @colors.map { |c| c.red } greens = @colors.map { |c| c.green } blues = @colors.map { |c| c.blue } multiple_regression(reds, greens, blues) end |
#include?(color) ⇒ Boolean
Test if a Color exists in the receiver
94 95 96 |
# File 'lib/paleta/palette.rb', line 94 def include?(color) @colors.include?(color) end |
#invert! ⇒ Palette
Invert each Color in the receiver by a percentage
116 117 118 119 |
# File 'lib/paleta/palette.rb', line 116 def invert! @colors.each { |color| color.invert! } self end |
#lighten!(percentage = 5) ⇒ Palette
Lighen each Color in the receiver by a percentage
101 102 103 104 |
# File 'lib/paleta/palette.rb', line 101 def lighten!(percentage = 5) @colors.each { |color| color.lighten!(percentage) } self end |
#pop ⇒ Object
Remove the most recently added Color from the receiver
50 51 52 |
# File 'lib/paleta/palette.rb', line 50 def pop @colors.pop end |
#push(color) ⇒ Palette #push(palette) ⇒ Palette
Add a Color to the Paleta::Palette
45 46 47 |
# File 'lib/paleta/palette.rb', line 45 def push(obj) self << obj end |
#similarity(palette) ⇒ Number
Calculate the similarity between the receiver and another Paleta::Palette
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/paleta/palette.rb', line 124 def similarity(palette) r, a, b = [], [], [] (0..1).each { |i| a[i], b[i] = {}, {} } # r[i] is a hash of the multiple regression of the Palette in RGB space r[0] = fit r[1] = palette.fit [0, 1].each do |i| [:x, :y, :z].each do |k| a[i][k] = 0 * r[i][:slope][k] + r[i][:offset][k] b[i][k] = 255 * r[i][:slope][k] + r[i][:offset][k] end end d_max = sqrt(3 * (65025 ** 2)) d1 = distance(a[0], a[1]) / d_max d2 = distance(b[0], b[1]) / d_max d1 + d2 end |
#size ⇒ Number
The number of Colors in the Paleta::Palette
68 69 70 |
# File 'lib/paleta/palette.rb', line 68 def size @colors.size end |
#sort(&blk) ⇒ Palette
Create a new instance of Paleta::Palette that is a sorted copy of the receiver
79 80 81 82 |
# File 'lib/paleta/palette.rb', line 79 def sort(&blk) @colors.sort(&blk) Paleta::Palette.new(@colors) end |
#sort!(&blk) ⇒ Object
Sort the Colors in the receiver return [Palette] self
86 87 88 89 |
# File 'lib/paleta/palette.rb', line 86 def sort!(&blk) @colors.sort!(&blk) self end |
#to_array(color_model = :rgb) ⇒ Array
Return an array representation of a Paleta::Palette instance,
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/paleta/palette.rb', line 190 def to_array(color_model = :rgb) color_model = color_model.to_sym unless color_model.is_a? Symbol if [:rgb, :hsl].include?(color_model) array = colors.map { |c| c.to_array(color_model) } elsif color_model == :hex array = colors.map{ |c| c.hex } else raise(ArgumentError, "Argument must be :rgb, :hsl, or :hex") end array end |