From 1af801f54ad18f2d0fcb7dba90081686ec96b169 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 25 Aug 2019 15:44:14 +0800 Subject: [PATCH] add px_gray --- src/pixel/px_gray.scad | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/pixel/px_gray.scad diff --git a/src/pixel/px_gray.scad b/src/pixel/px_gray.scad new file mode 100644 index 00000000..3e5d49e3 --- /dev/null +++ b/src/pixel/px_gray.scad @@ -0,0 +1,36 @@ +function _px_gray_row(r_count, row_bits, width, height, center, invert) = + let( + half_w = width / 2, + half_h = height / 2, + offset_x = center ? 0 : half_w, + offset_y = center ? 0 : half_h, + level = invert ? 0 : 255 + ) + [ + for(i = 0; i < width; i = i + 1) + if(row_bits[i] != level) + [ + i - half_w + offset_x, + r_count + offset_y, + invert ? row_bits[i] / 255 : 1 - row_bits[i] / 255 + ] + ]; + +function px_gray(levels, size, center = false, invert = false) = + let( + squar = sqrt(len(levels)), + siz = is_undef(size) ? [squar, squar] : size, + width = siz[0], + height = siz[1], + rows = [ + for(row = height - 1; row > -1; row = row - 1) + [for(column = 0; column < width; column = column + 1) + levels[row * width + column]] + ], + offset_i = height / 2 + ) + [ + for(i = 0; i < height; i = i + 1) + let(row = _px_gray_row(i - offset_i, rows[i], width, height, center, invert)) + if(row != []) each row + ]; \ No newline at end of file