Код:
-- floyd steinberg dither --
function Dither.floyd_steinberg_dither(matrix, bool_matrix, width, height)
for y=1, height-1 do
for x=2, width-1 do
local old_r,old_g,old_b = colors.toRGB(matrix[y][x])
--old_r,old_g,old_b = Dither.greyscale_pixel(old_r,old_g,old_b)
local factor = 3
local new_r = CEIL(factor * old_r / 255) * (255 / factor)
local new_g = CEIL(factor * old_g / 255) * (255 / factor)
local new_b = CEIL(factor * old_b / 255) * (255 / factor)
new_r,new_g,new_b = math.floor(new_r), math.floor(new_g), math.floor(new_b)
matrix[y][x] = colors.fromRGB(new_r,new_g,new_b)
bool_matrix[CEIL(y/3)][CEIL(x/2)] = true
local err_r = old_r - new_r
local err_g = old_g - new_g
local err_b = old_b - new_b
local index_x, index_y = x+1, y
local c_r, c_g, c_b = colors.toRGB(matrix[index_y][index_x])
c_r = c_r + err_r * 7/16.0
c_g = c_g + err_g * 7/16.0
c_b = c_b + err_b * 7/16.0
matrix[index_y][index_x] = colors.fromRGB(c_r, c_g, c_b)
bool_matrix[CEIL(index_y/3)][CEIL(index_x/2)] = true
index_x, index_y = x-1, y+1
c_r, c_g, c_b = colors.toRGB(matrix[index_y][index_x])
c_r = c_r + err_r * 3/16.0
c_g = c_g + err_g * 3/16.0
c_b = c_b + err_b * 3/16.0
matrix[index_y][index_x] = colors.fromRGB(c_r, c_g, c_b)
bool_matrix[CEIL(index_y/3)][CEIL(index_x/2)] = true
index_x, index_y = x, y+1
c_r, c_g, c_b = colors.toRGB(matrix[index_y][index_x])
c_r = c_r + err_r * 5/16.0
c_g = c_g + err_g * 5/16.0
c_b = c_b + err_b * 5/16.0
matrix[index_y][index_x] = colors.fromRGB(c_r, c_g, c_b)
bool_matrix[CEIL(index_y/3)][CEIL(index_x/2)] = true
index_x, index_y = x+1, y+1
c_r, c_g, c_b = colors.toRGB(matrix[index_y][index_x])
c_r = c_r + err_r * 1/16.0
c_g = c_g + err_g * 1/16.0
c_b = c_b + err_b * 1/16.0
matrix[index_y][index_x] = colors.fromRGB(c_r, c_g, c_b)
bool_matrix[CEIL(index_y/3)][CEIL(index_x/2)] = true
--[[
[y+1][x ]
[y+1][x+1]
]]
end
end
return matrix, bool_matrix
end
colors взял отсюда:
https://www.computercraft.info/forums2/index.php?/topic/23048-rgb-api-convert-rgb-colors-to-computercraft-colors-and-back/