Module: Moon::PaletteParser

Defined in:
lib/moon/packages/std/palette_parser.rb

Defined Under Namespace

Classes: FormatError

Class Method Summary collapse

Class Method Details

.load_palette(data) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/moon/packages/std/palette_parser.rb', line 61

def self.load_palette(data)
  colors = data.fetch("colors")
  format = data.fetch("format")
  case format
  when "hex.RGB"
    load_palette_hex_rgb colors
  when "hex.RRGGBB"
    load_palette_hex_rrggbb colors
  when "[rgb]"
    load_palette_array_rgb colors
  when "[rgba]"
    load_palette_array_rgba colors
  when "float[rgb]"
    load_palette_float_array_rgb colors
  when "float[rgba]"
    load_palette_float_array_rgba colors
  else
    raise FormatError, "unsupported palette format #{format}"
  end
end

.load_palette_array_rgb(colors) ⇒ Object


29
30
31
32
33
34
35
# File 'lib/moon/packages/std/palette_parser.rb', line 29

def self.load_palette_array_rgb(colors)
  colors.each_with_object({}) do |a, hash|
    key, ary = *a
    r, g, b = *ary
    hash[key] = Moon::Vector4.new(r/255.0, g/255.0, b/255.0, 1.0)
  end
end

.load_palette_array_rgba(colors) ⇒ Object


37
38
39
40
41
42
43
# File 'lib/moon/packages/std/palette_parser.rb', line 37

def self.load_palette_array_rgba(colors)
  colors.each_with_object({}) do |a, hash|
    key, ary = *a
    r, g, b, a = *ary
    hash[key] = Moon::Vector4.new(r/255.0,g/255.0,b/255.0,a/255.0)
  end
end

.load_palette_float_array_rgb(colors) ⇒ Object


45
46
47
48
49
50
51
# File 'lib/moon/packages/std/palette_parser.rb', line 45

def self.load_palette_float_array_rgb(colors)
  colors.each_with_object({}) do |a, hash|
    key, ary = *a
    r, g, b = *ary
    hash[key] = Moon::Vector4.new(r,g,b,1.0)
  end
end

.load_palette_float_array_rgba(colors) ⇒ Object


53
54
55
56
57
58
59
# File 'lib/moon/packages/std/palette_parser.rb', line 53

def self.load_palette_float_array_rgba(colors)
  colors.each_with_object({}) do |a, hash|
    key, ary = *a
    r, g, b, a = *ary
    hash[key] = Moon::Vector4.new(r,g,b,a)
  end
end

.load_palette_hex(colors, bits, format_rgx, format_str) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/moon/packages/std/palette_parser.rb', line 6

def self.load_palette_hex(colors, bits, format_rgx, format_str)
  bpc = (bits / 3).to_i # bits per channel
  bitmask = 2 ** bpc - 1
  colors.each_with_object({}) do |a, hash|
    key, hex = *a
    matchdata = hex.match(format_rgx)
    raise FormatError, "invalid hex format (#{hex}) expected (#{format_str})" unless matchdata
    value = matchdata[0].to_i(16)
    r = (value >> (bpc * 2)) & bitmask
    g = (value >> bpc) & bitmask
    b = value & bitmask
    hash[key] = Moon::Vector4.new(r/bitmask.to_f, g/bitmask.to_f, b/bitmask.to_f, 1.0)
  end
end

.load_palette_hex_rgb(colors) ⇒ Object


21
22
23
# File 'lib/moon/packages/std/palette_parser.rb', line 21

def self.load_palette_hex_rgb(colors)
  load_palette_hex(colors, 12, /[0-9a-fA-F]{3}/, "RGB")
end

.load_palette_hex_rrggbb(colors) ⇒ Object


25
26
27
# File 'lib/moon/packages/std/palette_parser.rb', line 25

def self.load_palette_hex_rrggbb(colors)
  load_palette_hex(colors, 24, /[0-9a-fA-F]{6}/, "RRGGBB")
end