module ASEPalette::PaletteBinary

Constants

BLOCK_TYPE_COLOR
BLOCK_TYPE_GROUP_END
BLOCK_TYPE_GROUP_START
COLOR_MODEL_CMYK
COLOR_MODEL_DEFAULT
COLOR_MODEL_GRAY
COLOR_MODEL_LAB
COLOR_MODEL_RGB
COLOR_TYPE_DEFAULT
COLOR_TYPE_GLOBAL
COLOR_TYPE_NORMAL
COLOR_TYPE_SPOT
DEFAULT_VERSION_MAJOR
DEFAULT_VERSION_MINOR

Public Class Methods

build_binary_hash_from_file(path) click to toggle source
# File lib/ase-palette/palette_binary.rb, line 137
def self.build_binary_hash_from_file(path)
  file = File.open(path)
  binary = ASEBinData.read(file)
  binary_to_hash(binary)
end
build_binary_palette( colors, groups, version_major = DEFAULT_VERSION_MAJOR, version_minor = DEFAULT_VERSION_MINOR ) click to toggle source
# File lib/ase-palette/palette_binary.rb, line 103
def self.build_binary_palette(
  colors,
  groups,
  version_major = DEFAULT_VERSION_MAJOR,
  version_minor = DEFAULT_VERSION_MINOR
)
  palette = ASEBinData.new
  palette.version_major = version_major
  palette.version_minor = version_minor
  colors.each do |color|
    binary_add_color(
      palette,
      color[:name],
      color[:model],
      color[:type],
      color[:data],
    )
  end
  groups.each do |group|
    binary_begin_group(palette, group[:name])
    group[:colors].each do |color|
      binary_add_color(
        palette,
        color[:name],
        color[:model],
        color[:type],
        color[:data],
      )
    end
    binary_end_group(palette)
  end
  palette
end

Private Class Methods

binary_add_color(palette, name, model, type, data) click to toggle source
# File lib/ase-palette/palette_binary.rb, line 145
def self.binary_add_color(palette, name, model, type, data)
  case model
  when :rgb
    color_model = COLOR_MODEL_RGB
    color_data = {
      red: data[:r] / 255.0,
      green: data[:g] / 255.0,
      blue: data[:b] / 255.0,
    }
  when :cmyk
    color_model = COLOR_MODEL_CMYK
    color_data = {
      cyan: data[:c] / 100.0,
      magenta: data[:m] / 100.0,
      yellow: data[:y] / 100.0,
      black: data[:k] / 100.0,
    }
  when :lab
    color_model = COLOR_MODEL_LAB
    color_data = {
      lightness: data[:l] / 100.0,
      a: data[:a],
      b: data[:b],
    }
  when :gray
    # Grayscale is no longer supported by Adobe (was it ever?)
    # This will default to a CMYK value with data only for black
    color_model = COLOR_MODEL_CMYK
    color_data = {
      cyan: 0.0,
      magenta: 0.0,
      yellow: 0.0,
      black: data[:gray] / 100.0,
    }
  end

  case type
  when :global
    color_type = COLOR_TYPE_GLOBAL
  when :spot
    color_type = COLOR_TYPE_SPOT
  when :normal
    color_type = COLOR_TYPE_NORMAL
  end

  palette.blocks.push({
    block_type: BLOCK_TYPE_COLOR,
    block_data: {
      name: name.encode(Encoding::UTF_16BE),
      color_model: color_model,
      color_data: color_data,
      color_type: color_type,
    }
  })
end
binary_begin_group(palette, name) click to toggle source
# File lib/ase-palette/palette_binary.rb, line 201
def self.binary_begin_group(palette, name)
  palette.blocks.push({
    block_type: BLOCK_TYPE_GROUP_START,
    block_data: {
      name: name.encode(Encoding::UTF_16BE),
    }
  })
end
binary_end_group(palette) click to toggle source
# File lib/ase-palette/palette_binary.rb, line 210
def self.binary_end_group(palette)
  palette.blocks.push({
    block_type: BLOCK_TYPE_GROUP_END,
  })
end
binary_to_hash(binary) click to toggle source
# File lib/ase-palette/palette_binary.rb, line 216
def self.binary_to_hash(binary)
  palette = {
    version_major: binary.version_major,
    version_minor: binary.version_minor,
    colors: [],
    groups: [],
  }

  current_group = nil
  binary.blocks.each do |block|
    block_type = block[:block_type]
    block_data = block[:block_data]
    if block_type == BLOCK_TYPE_COLOR || block_type == BLOCK_TYPE_GROUP_START
      block_name = block_data[:name].force_encoding(Encoding::UTF_16BE).encode(Encoding::UTF_8)
    end
    if block_type == BLOCK_TYPE_COLOR
      color = { name: block_name }

      color_data = block_data[:color_data]

      case block_data[:color_model]
      when COLOR_MODEL_RGB
        color[:model] = :rgb
        color[:data] = {
          r: (color_data[:red] * 255.0).round,
          g: (color_data[:green] * 255.0).round,
          b: (color_data[:blue] * 255.0).round,
        }
      when COLOR_MODEL_CMYK, COLOR_MODEL_GRAY
        color[:model] = :cmyk
        color[:data] = {
          c: (color_data[:cyan] * 100.0).round,
          m: (color_data[:magenta] * 100.0).round,
          y: (color_data[:yellow] * 100.0).round,
          k: (color_data[:black] * 100.0).round,
        }
      when COLOR_MODEL_LAB
        color[:model] = :lab
        color[:data] = {
          l: (color_data[:lightness] * 100.0).round,
          a: (color_data[:a]).round,
          b: (color_data[:b]).round,
        }
      end

      case block_data[:color_type]
      when COLOR_TYPE_GLOBAL
        color[:type] = :global
      when COLOR_TYPE_SPOT
        color[:type] = :spot
      when COLOR_TYPE_NORMAL
        color[:type] = :normal
      end

      if current_group
        current_group[:colors] << color
      else
        palette[:colors] << color
      end
    elsif block_type == BLOCK_TYPE_GROUP_START
      current_group = {
        name: block_name,
        colors: []
      }
      palette[:groups] << current_group
    elsif block_type == BLOCK_TYPE_GROUP_END
      current_group = nil
    end
  end

  palette
end