Created
December 31, 2009 19:54
-
-
Save aberant/266880 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# getting more power out of Enumerable | |
# this is to convert an RGB data structure, into RGBA | |
require 'enumerator' | |
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.inject([]){|result, pixel| result = result + pixel + [0] } | |
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