class ASEPalette::Palette
Public Class Methods
Initialize palette
# File lib/ase-palette/palette.rb, line 6 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
Public Instance Methods
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
# File lib/ase-palette/palette.rb, line 59 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' is not of type #{ASEPalette::Color}" end end
Get color by name
# File lib/ase-palette/palette.rb, line 39 def color_with_name(name) found_colors = all_colors.select { |color| color.name == name } found_colors.length >= 1 ? found_colors[0] : nil end
Get read-only list of colors Optionally include all colors from groups
# File lib/ase-palette/palette.rb, line 30 def colors(include_from_groups: false) if include_from_groups all_colors else @colors.clone end end
Create empty group in palette Returns true if group is created
# File lib/ase-palette/palette.rb, line 80 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
Get read-only group by name
# File lib/ase-palette/palette.rb, line 50 def group_with_name(name) found_groups = @groups.select { |group| group.name == name } found_groups.length >= 1 ? found_groups[0].clone : nil end
Get read-only list of groups
# File lib/ase-palette/palette.rb, line 45 def groups @groups.clone end
Remove color from palette Color
may or may not be in a group
# File lib/ase-palette/palette.rb, line 91 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, and its colors, from palette
# File lib/ase-palette/palette.rb, line 98 def remove_group_with_name(name) @groups = @groups.select { |group| group.name != name } true end
Set palette version
# File lib/ase-palette/palette.rb, line 23 def set_version(major, minor) @version_major = major @version_minor = minor end
Create binary representation of palette
# File lib/ase-palette/palette.rb, line 129 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
Create human-readable hex representation of palette
# File lib/ase-palette/palette.rb, line 140 def to_hex to_binary.to_hex_string end
Create string representation of palette
# File lib/ase-palette/palette.rb, line 104 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
Get palette version
# File lib/ase-palette/palette.rb, line 18 def version "#{@version_major}.#{@version_minor}" end
Private Instance Methods
Returns an array of all colors in the palette, including those in groups
# File lib/ase-palette/palette.rb, line 188 def all_colors @colors + @groups.map { |group| group.colors }.flatten end
Determines whether or not a color exists in the palette, including those in groups
# File lib/ase-palette/palette.rb, line 194 def color_does_not_exist(name) all_colors.select { |color| color.name == name }.length == 0 end
Create Color
object from binary-derived hash Used to populate a Palette
from an ASE file
# File lib/ase-palette/palette.rb, line 163 def color_from_hash(color) case color[:model] when :rgb Color::RGB.new( color[:name], *color[:data].values, color[:type], ) when :cmyk Color::CMYK.new( color[:name], *color[:data].values, color[:type], ) when :lab Color::LAB.new( color[:name], *color[:data].values, color[:type], ) end end
Returns a found or created group
# File lib/ase-palette/palette.rb, line 204 def find_or_create_group(name) found_groups = @groups.select { |group| group.name == name } if found_groups.length > 0 found_groups[0] else new_group = ASEPalette::Group.new(name) @groups << new_group new_group end end
Determines whether or not a group exists in the palette
# File lib/ase-palette/palette.rb, line 199 def group_does_not_exist(name) @groups.select { |group| group.name == name }.length == 0 end
Set palette values from binary-derived hash Used to populate a Palette
from an ASE file
# File lib/ase-palette/palette.rb, line 148 def initialize_values_from_palette_hash(palette_hash) @version_major = palette_hash[:version_major] @version_minor = palette_hash[:version_minor] palette_hash[:colors].each do |color| add_color(color_from_hash(color)) end palette_hash[:groups].each do |group| group[:colors].each do |color| add_color(color_from_hash(color), group_name: group[:name]) end end end