Skip to content

Instantly share code, notes, and snippets.

@ryanbriones
Forked from aberant/gist:266880
Created December 31, 2009 23:51
Show Gist options
  • Save ryanbriones/266968 to your computer and use it in GitHub Desktop.
Save ryanbriones/266968 to your computer and use it in GitHub Desktop.
# getting more power out of Enumerable
# this is to convert an RGB data structure, into RGBA
RGB_WIDTH = 3
raw_data = [1,1,1,2,2,2,3,3,3,4,4,4]
# returns an enumerable object with the raw data split up into chunks of 3
pixels = raw_data.enum_slice( RGB_WIDTH )
# give each pixel an extra alpha byte
rgba_result = pixels.zip([0]*(raw_data.size / RGB_WIDTH)).flatten
puts rgba_result.inspect
# [1,1,1,0,2,2,2,0,3,3,3,0,4,4,4,0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment