Module: GDAL::RasterBand::ColoringExtensions
- Included in:
- GDAL::RasterBand
- Defined in:
- lib/gdal/extensions/raster_band/coloring_extensions.rb
Overview
RasterBand methods added for dealing with colorizing.
Instance Method Summary collapse
-
#colorize!(*colors) ⇒ Object
Sets the band to be a Palette band, then applies an RGB color table using the given colors.
-
#colors_as_hex ⇒ Array<String>
Gets the colors from the associated ColorTable and returns an Array of Strings, where the RGB color for each ColorEntry has been converted to Hex.
-
#colors_as_rgb ⇒ Array<Array<Integer>>
Gets the colors from the associated ColorTable and returns an Array of those, where each ColorEntry is [R, G, B, A].
- #hex_to_rgb(hex) ⇒ Object
Instance Method Details
#colorize!(*colors) ⇒ Object
Sets the band to be a Palette band, then applies an RGB color table using the given colors. Colors are distributed evenly across the table based on the number of colors given. Note that this will overwrite any existing color table that may be set on this band.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gdal/extensions/raster_band/coloring_extensions.rb', line 23 def colorize!(*colors) return if colors.empty? self.color_interpretation ||= :GCI_PaletteIndex table = GDAL::ColorTable.new(:GPI_RGB) table.add_color_entry(0, 0, 0, 0, 255) # Start at 1 instead of 0 because we manually set the first color entry # to white. color_entry_index_range = case data_type when :GDT_Byte then 1..255 when :GDT_UInt16 then 1..65_535 else raise "Can't colorize a #{data_type} band--must be :GDT_Byte or :GDT_UInt16" end bin_count = (color_entry_index_range.last + 1) / colors.size.to_f color_entry_index_range.step do |color_entry_index| color_number = (color_entry_index / bin_count).floor.to_i color = colors[color_number] # TODO: Fix possible uninitialized rgb_array rgb_array = hex_to_rgb(color) unless color.is_a?(Array) table.add_color_entry(color_entry_index, rgb_array[0], rgb_array[1], rgb_array[2], 255) end self.color_table = table end |
#colors_as_hex ⇒ Array<String>
Gets the colors from the associated ColorTable and returns an Array of Strings, where the RGB color for each ColorEntry has been converted to Hex.
69 70 71 72 73 74 75 |
# File 'lib/gdal/extensions/raster_band/coloring_extensions.rb', line 69 def colors_as_hex colors_as_rgb.map do |rgba| rgb = rgba.to_a[0..2] "##{rgb[0].to_s(16)}#{rgb[1].to_s(16)}#{rgb[2].to_s(16)}" end end |
#colors_as_rgb ⇒ Array<Array<Integer>>
Gets the colors from the associated ColorTable and returns an Array of those, where each ColorEntry is [R, G, B, A].
58 59 60 61 62 |
# File 'lib/gdal/extensions/raster_band/coloring_extensions.rb', line 58 def colors_as_rgb return [] unless color_table color_table.color_entries_as_rgb.map(&:to_a) end |
#hex_to_rgb(hex) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/gdal/extensions/raster_band/coloring_extensions.rb', line 78 def hex_to_rgb(hex) hex = hex.sub(/^#/, "") matches = hex.match(/(?<red>[a-zA-Z0-9]{2})(?<green>[a-zA-Z0-9]{2})(?<blue>[a-zA-Z0-9]{2})/) [matches[:red].to_i(16), matches[:green].to_i(16), matches[:blue].to_i(16)] end |