
An then, using use_palette, that is equal to the number of the row, and now knowing also the column, we take the color of the palette and place it in the cords (x,y) on image_data. We first take the cords (x,y) of the color to be change, we search the color on the image_map using the method getPixel(x,y), after we transform the color to an id, and use it to access to the table look_up_color_table, that returns the column number.

Now, in the function changeColors() happens everything. newImage ( image_data ) image : setFilter ( 'nearest', 'nearest' ) end function changePalete () image_data : mapPixel ( changeColors ) image = love. mapPixel() allow us to send a function that will be run for each one of the pixels on the object, in this case, the very function that actually change the colors.Īfter, as objects of type ImageData can not be draw, we should create a image object to draw on screen, and we set the filter to “nearest”. What happens inside of the function changePalete(), the method mapPixel() of the object ImageData is called. We fixed the palette to be used and change the colors on image_data. look_up_color_table = local col = palette_data : getWidth () local i = 0 while i < col do local r, g, b, a = palette_data : getPixel ( i, 0 ) -the id is created using a hex like value local id = ( "%X_%X_%X_%X" ): format ( math.floor (( r ) * 255 ), math.floor (( g ) * 255 ), math.floor (( b ) * 255 ), math.floor (( a ) * 255 )) look_up_color_table = i i = i + 1 end We also gonna make a string that will be used as an id to identify each color quickly. This table will be called look_up_color_table. Now we create a new table to be used as look up where each column of the first row of the palette image corresponds to each color of the first palette. max_palettes = palette_data : getHeight () We also must calculate the max number of palettes that we can use ( max_palettes), that will be of the same height as the image were are stored the palettes. newImageData ( image_map : getWidth (), image_map : getHeight ()) palette_data = love. newImageData ( 'ima.png' ) image_data = love. There has to be load an image that contains the palettes to be used, the palette_data, where each row of pixels is a palette. We also make an image of the same size where we will write all the changes to be made, and call it image_data 1. The algorithm is based on load to the memory our image as an object of type ImageData, in which we will do not any changes and only use as a ” map “ to know what is the color to be used, for that reason we gonna call it image_map. Is possible change the color of the images using the capacity of ImageData to access directly the pixels, to both, read the colors, as to modify they. Changing the color of a image can be a very useful form to reuse an asset without rise the file size of the game on disk.
