Module: RbSDL2::PixelFormatEnum

Included in:
DisplayMode, Surface, Surface::PixelFormat, Window
Defined in:
lib/rb_sdl2/pixel_format_enum.rb

Constant Summary collapse

FORMAT_MAP =
table.map { |sym, n, _, _| [sym, n] }.to_h.freeze
INDEXED_TYPES =
table.inject([]) { |a, (_, n, type, _)| a << n if type == :indexed; a }.freeze
WITH_ALPHA =
table.inject([]) { |a, (_, n, _, alpha)| a << n if alpha; a }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_name(num) ⇒ Object



70
71
72
# File 'lib/rb_sdl2/pixel_format_enum.rb', line 70

def to_name(num)
  ::SDL.GetPixelFormatName(num).read_string.delete_prefix("SDL_PIXELFORMAT_")
end

.to_num(obj) ⇒ Object

obj に与えたピクセルフォーマットに応じた整数値を戻す。



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rb_sdl2/pixel_format_enum.rb', line 75

def to_num(obj)
  case obj
  when Symbol, String
    sym = obj.to_sym.downcase
    if FORMAT_MAP.key?(sym)
      FORMAT_MAP[sym]
    else
      raise ArgumentError, "Invalid format name"
    end
  else
    raise ArgumentError
  end
end

Instance Method Details

#format_nameObject



97
# File 'lib/rb_sdl2/pixel_format_enum.rb', line 97

def format_name = PixelFormatEnum.to_num(format)

#fourccObject

FOURCC が無い場合は nil を戻す。



91
92
93
# File 'lib/rb_sdl2/pixel_format_enum.rb', line 91

def fourcc
  4.times.inject([]) { |n, i| n << (num >> i * 8) % 0x100 }.pack("c4") if fourcc?
end

#fourcc?Boolean

Returns:

  • (Boolean)


95
# File 'lib/rb_sdl2/pixel_format_enum.rb', line 95

def fourcc? = (format >> 28) & 0x0F != 1

#indexed_color?Boolean

Returns:

  • (Boolean)


99
# File 'lib/rb_sdl2/pixel_format_enum.rb', line 99

def indexed_color? = PixelFormatEnum::INDEXED_TYPES.include?(format)

#rgb?Boolean

Returns:

  • (Boolean)


101
# File 'lib/rb_sdl2/pixel_format_enum.rb', line 101

def rgb? = !(fourcc? || indexed_color? || rgba?)

#rgba?Boolean

Returns:

  • (Boolean)


103
# File 'lib/rb_sdl2/pixel_format_enum.rb', line 103

def rgba? = PixelFormatEnum::WITH_ALPHA.include?(format)