Module: SkippyLib::ImageRepHelper

Extended by:
ImageRepHelper
Included in:
ImageRepHelper
Defined in:
modules/image_rep.rb

Overview

Since:

  • 3.0.0

SketchUp:

  • 2018

Class Method Summary collapse

Class Method Details

.color_to_24bit(color) ⇒ Array(Integer, Integer, Integer)

Returns RGBA/BGRA.

Parameters:

  • color (Array<Sketchup::Color>)

Returns:

  • (Array(Integer, Integer, Integer))

    RGBA/BGRA

Since:

  • 3.0.0



52
53
54
# File 'modules/image_rep.rb', line 52

def self.color_to_24bit(color)
  self.color_to_32bit(color)[0, 3]
end

.color_to_32bit(color) ⇒ Array(Integer, Integer, Integer, Integer)

From C API documentation on SUColorOrder

SketchUpAPI expects the channels to be in different orders on Windows vs. Mac OS. Bitmap data is exposed in BGRA and RGBA byte orders on Windows and Mac OS, respectively.

Parameters:

  • color (Array<Sketchup::Color>)

Returns:

  • (Array(Integer, Integer, Integer, Integer))

    RGBA/BGRA

Since:

  • 3.0.0



37
38
39
40
# File 'modules/image_rep.rb', line 37

def self.color_to_32bit(color)
  r, g, b, a = color.to_a
  Platform::IS_WIN ? [b, g, r, a] : [r, g, b, a]
end

.colors_to_24bit_bytes(colors) ⇒ String

Returns Binary byte string of raw image data.

Parameters:

  • colors (Array<Sketchup::Color>)

Returns:

  • (String)

    Binary byte string of raw image data.

Since:

  • 3.0.0



59
60
61
# File 'modules/image_rep.rb', line 59

def self.colors_to_24bit_bytes(colors)
  colors.map { |color| self.color_to_24bit(color) }.flatten.pack('C*')
end

.colors_to_32bit_bytes(colors) ⇒ String

Returns Binary byte string of raw image data.

Parameters:

  • colors (Array<Sketchup::Color>)

Returns:

  • (String)

    Binary byte string of raw image data.

Since:

  • 3.0.0



45
46
47
# File 'modules/image_rep.rb', line 45

def self.colors_to_32bit_bytes(colors)
  colors.map { |color| self.color_to_32bit(color) }.flatten.pack('C*')
end

.colors_to_image_rep(width, height, colors) ⇒ Sketchup::ImageRep

Parameters:

  • width (Integer)
  • height (Integer)
  • colors (Array<Sketchup::Color>)

Returns:

  • (Sketchup::ImageRep)

Since:

  • 3.0.0



17
18
19
20
21
22
23
24
25
26
# File 'modules/image_rep.rb', line 17

def self.colors_to_image_rep(width, height, colors)
  row_padding = 0
  bits_per_pixel = 32
  pixel_data = self.colors_to_32bit_bytes(colors)
  # rubocop:disable SketchupSuggestions/Compatibility
  image_rep = Sketchup::ImageRep.new
  # rubocop:enable SketchupSuggestions/Compatibility
  image_rep.set_data(width, height, bits_per_pixel, row_padding, pixel_data)
  image_rep
end