Class: ASEPalette::Palette
- Inherits:
-
Object
- Object
- ASEPalette::Palette
- Defined in:
- lib/ase-palette/palette.rb
Instance Method Summary collapse
-
#add_color(color, group_name: nil) ⇒ Object
Add color to palette Optionally provide 'group_name' to place color in group Group will be created if it does not exist Returns true if color is added.
-
#add_colors(colors, group_name: nil) ⇒ Object
Add multiple colors to palette 'colors' must be an array of Color objects Optionally provide 'group_name' to place color in group Group will be created if it does not exist.
-
#color_with_name(name) ⇒ Object
Get color by name.
-
#colors(include_from_groups: false) ⇒ Object
Get read-only list of colors Optionally include all colors from groups.
-
#create_group(name) ⇒ Object
Create empty group in palette Returns true if group is created.
-
#group_with_name(name) ⇒ Object
Get read-only group by name.
-
#groups ⇒ Object
Get read-only list of groups.
-
#initialize(path = nil) ⇒ Palette
constructor
Initialize palette.
-
#remove_color_with_name(name) ⇒ Object
Remove color from palette Color may or may not be in a group.
-
#remove_group_with_name(name) ⇒ Object
Remove group, and its colors, from palette.
-
#set_version(major, minor) ⇒ Object
Set palette version.
-
#to_binary ⇒ Object
Create binary representation of palette.
-
#to_hex ⇒ Object
Create human-readable hex representation of palette.
-
#to_s ⇒ Object
Create string representation of palette.
-
#version ⇒ Object
Get palette version.
Constructor Details
#initialize(path = nil) ⇒ Palette
Initialize palette
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/ase-palette/palette.rb', line 4 def initialize(path = nil) @version_major = 1 @version_minor = 0 @colors = [] @groups = [] if path palette_hash = PaletteBinary.build_binary_hash_from_file(path) initialize_values_from_palette_hash(palette_hash) end end |
Instance Method Details
#add_color(color, group_name: nil) ⇒ Object
Add color to palette Optionally provide 'group_name' to place color in group Group will be created if it does not exist Returns true if color is added
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ase-palette/palette.rb', line 57 def add_color(color, group_name: nil) if color.is_a? Color if color_does_not_exist(color.name) if group_name group = find_or_create_group(group_name) group.colors << color true else @colors << color true end else raise Error, "A color named #{color.name} already exists" end else raise Error, "Argument 'color' must be of type #{ASEPalette::Color}" end end |
#add_colors(colors, group_name: nil) ⇒ Object
Add multiple colors to palette 'colors' must be an array of Color objects Optionally provide 'group_name' to place color in group Group will be created if it does not exist
80 81 82 83 84 85 86 87 88 |
# File 'lib/ase-palette/palette.rb', line 80 def add_colors(colors, group_name: nil) if colors.is_a? Array colors.each do |color| add_color(color, group_name: group_name) end else raise Error, "Argument 'colors' must be of type #{Array}" end end |
#color_with_name(name) ⇒ Object
Get color by name
37 38 39 40 |
# File 'lib/ase-palette/palette.rb', line 37 def color_with_name(name) found_colors = all_colors.select { |color| color.name == name } found_colors.length >= 1 ? found_colors[0] : nil end |
#colors(include_from_groups: false) ⇒ Object
Get read-only list of colors Optionally include all colors from groups
28 29 30 31 32 33 34 |
# File 'lib/ase-palette/palette.rb', line 28 def colors(include_from_groups: false) if include_from_groups all_colors else @colors.clone end end |
#create_group(name) ⇒ Object
Create empty group in palette Returns true if group is created
92 93 94 95 96 97 98 99 |
# File 'lib/ase-palette/palette.rb', line 92 def create_group(name) if group_does_not_exist(name) @groups << ASEPalette::Group.new(name) true else raise Error, "A group named #{name} already exists" end end |
#group_with_name(name) ⇒ Object
Get read-only group by name
48 49 50 51 |
# File 'lib/ase-palette/palette.rb', line 48 def group_with_name(name) found_groups = @groups.select { |group| group.name == name } found_groups.length >= 1 ? found_groups[0].clone : nil end |
#groups ⇒ Object
Get read-only list of groups
43 44 45 |
# File 'lib/ase-palette/palette.rb', line 43 def groups @groups.clone end |
#remove_color_with_name(name) ⇒ Object
Remove color from palette Color may or may not be in a group
103 104 105 106 107 |
# File 'lib/ase-palette/palette.rb', line 103 def remove_color_with_name(name) @colors = @colors.select { |color| color.name != name } @groups.each { |group| group.remove_color_with_name(name) } true end |
#remove_group_with_name(name) ⇒ Object
Remove group, and its colors, from palette
110 111 112 113 |
# File 'lib/ase-palette/palette.rb', line 110 def remove_group_with_name(name) @groups = @groups.select { |group| group.name != name } true end |
#set_version(major, minor) ⇒ Object
Set palette version
21 22 23 24 |
# File 'lib/ase-palette/palette.rb', line 21 def set_version(major, minor) @version_major = major @version_minor = minor end |
#to_binary ⇒ Object
Create binary representation of palette
141 142 143 144 145 146 147 148 149 |
# File 'lib/ase-palette/palette.rb', line 141 def to_binary binary_palette = PaletteBinary.build_binary_palette( @colors.map(&:to_h), @groups.map(&:to_h), @version_major, @version_minor, ) binary_palette.to_binary_s end |
#to_hex ⇒ Object
Create human-readable hex representation of palette
152 153 154 |
# File 'lib/ase-palette/palette.rb', line 152 def to_hex to_binary.to_hex_string end |
#to_s ⇒ Object
Create string representation of palette
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/ase-palette/palette.rb', line 116 def to_s s = "ASEPalette #{version}\n" divider = "#{"-" * (s.length - 1)}\n" s += divider if @colors.length > 0 || @groups.length > 0 s += "\n" @colors.each do |color| s += "#{color}\n" end s += "\n" @groups.each do |group| s += "#{group}\n" end else s += "This palette is empty\n" end s += divider s += "#{all_colors.length} " \ "color#{if all_colors.length != 1 then "s" end}, " \ "#{@groups.length} " \ "group#{if @groups.length != 1 then "s" end}" s end |
#version ⇒ Object
Get palette version
16 17 18 |
# File 'lib/ase-palette/palette.rb', line 16 def version "#{@version_major}.#{@version_minor}" end |