From 3ea7fff85f5c0932153f67704010e04e20cde06e Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sun, 21 Jul 2019 09:20:04 +0900 Subject: [PATCH] Added (times i fn) to prelude --- desktop/sources/lisp/prelude.lisp | 27 +++-- desktop/sources/scripts/library.js | 154 ++++++++++++++--------------- desktop/sources/scripts/ronin.js | 2 +- examples/basics.lisp | 9 +- 4 files changed, 104 insertions(+), 88 deletions(-) diff --git a/desktop/sources/lisp/prelude.lisp b/desktop/sources/lisp/prelude.lisp index 82c0999..ef8b670 100644 --- a/desktop/sources/lisp/prelude.lisp +++ b/desktop/sources/lisp/prelude.lisp @@ -1,11 +1,20 @@ +; (echo "Loading prelude.lisp") - +; translate (defn translate - (r p) - (clone - r - (rect - (of p :x) - (of p :y) - (of r :w) - (of r :h)))) + (r p) + (clone r + (rect + (of p :x) + (of p :y) + (of r :w) + (of r :h)))) +; times +(defn times + (v f) + ( + (f v) + (if + (gt v 1) + (times + (sub v 1) f)))) \ No newline at end of file diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 0a8aa78..35b48b2 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -64,6 +64,83 @@ function Library (ronin) { return rect } + // Frame + + this.frame = () => { // Returns a rect of the frame. + return ronin.surface.getFrame() + } + + this.center = () => { // Returns a position of the center of the frame. + const rect = this.frame() + return this.pos(rect.w / 2, rect.h / 2) + } + + this.resize = async (w, h, fit = true) => { // Resizes the canvas to target w and h, returns the rect. + const rect = { x: 0, y: 0, w, h } + const a = document.createElement('img') + const b = document.createElement('img') + a.src = ronin.surface.el.toDataURL() + await ronin.surface.resizeImage(a, b) + ronin.surface.resize(rect, fit) + return ronin.surface.draw(b, rect) + } + + this.rescale = async (w, h) => { // Rescales the canvas to target ratio of w and h, returns the rect. + const rect = { x: 0, y: 0, w: this.frame().w * w, h: this.frame().h * h } + const a = document.createElement('img') + const b = document.createElement('img') + a.src = ronin.surface.el.toDataURL() + await ronin.surface.resizeImage(a, b) + ronin.surface.resize(rect, true) + return ronin.surface.draw(b, rect) + } + + this.crop = async (rect) => { // Crop canvas to rect. + return ronin.surface.crop(rect) + } + + this.clone = (a, b) => { + ronin.surface.clone(a, b) + return [a, b] + } + + this.theme = (variable, el = document.documentElement) => { + // ex. (theme "f_main") -> :root { --f_main: "#fff" } + return getComputedStyle(el).getPropertyValue(`--${variable}`) + } + + // Gradients + + this.gradient = ([x1, y1, x2, y2], colors = ['white', 'black']) => { + return ronin.surface.linearGradient(x1, y1, x2, y2, colors) + } + + // Pixels + + this.pixels = (rect, fn, q) => { + const img = ronin.surface.context.getImageData(0, 0, rect.w, rect.h) + for (let i = 0, loop = img.data.length; i < loop; i += 4) { + const pixel = { r: img.data[i], g: img.data[i + 1], b: img.data[i + 2], a: img.data[i + 3] } + const processed = fn(pixel, q) + img.data[i] = processed[0] + img.data[i + 1] = processed[1] + img.data[i + 2] = processed[2] + img.data[i + 3] = processed[3] + } + ronin.surface.context.putImageData(img, 0, 0) + return rect + } + + this.saturation = (pixel, q = 1) => { + const color = 0.2126 * pixel.r + 0.7152 * pixel.g + 0.0722 * pixel.b + return [(color * (1 - q)) + (pixel.r * q), (color * (1 - q)) + (pixel.g * q), (color * (1 - q)) + (pixel.b * q), pixel.a] + } + + this.contrast = (pixel, q = 1) => { + const intercept = 128 * (1 - q) + return [pixel.r * q + intercept, pixel.g * q + intercept, pixel.b * q + intercept, pixel.a] + } + // Strings this.concat = function (...items) { // Concat multiple strings. @@ -241,83 +318,6 @@ function Library (ronin) { return Object.values(item) } - // Frame - - this.frame = () => { // Returns a rect of the frame. - return ronin.surface.getFrame() - } - - this.center = () => { // Returns a position of the center of the frame. - const rect = this.frame() - return this.pos(rect.w / 2, rect.h / 2) - } - - this.resize = async (w, h, fit = true) => { // Resizes the canvas to target w and h, returns the rect. - const rect = { x: 0, y: 0, w, h } - const a = document.createElement('img') - const b = document.createElement('img') - a.src = ronin.surface.el.toDataURL() - await ronin.surface.resizeImage(a, b) - ronin.surface.resize(rect, fit) - return ronin.surface.draw(b, rect) - } - - this.rescale = async (w, h) => { // Rescales the canvas to target ratio of w and h, returns the rect. - const rect = { x: 0, y: 0, w: this.frame().w * w, h: this.frame().h * h } - const a = document.createElement('img') - const b = document.createElement('img') - a.src = ronin.surface.el.toDataURL() - await ronin.surface.resizeImage(a, b) - ronin.surface.resize(rect, true) - return ronin.surface.draw(b, rect) - } - - this.crop = async (rect) => { // Crop canvas to rect. - return ronin.surface.crop(rect) - } - - this.clone = (a, b) => { - ronin.surface.clone(a, b) - return [a, b] - } - - this.theme = (variable, el = document.documentElement) => { - // ex. (theme "f_main") -> :root { --f_main: "#fff" } - return getComputedStyle(el).getPropertyValue(`--${variable}`) - } - - // Gradients - - this.gradient = ([x1, y1, x2, y2], colors = ['white', 'black']) => { - return ronin.surface.linearGradient(x1, y1, x2, y2, colors) - } - - // Pixels - - this.pixels = (rect, fn, q) => { - const img = ronin.surface.context.getImageData(0, 0, rect.w, rect.h) - for (let i = 0, loop = img.data.length; i < loop; i += 4) { - const pixel = { r: img.data[i], g: img.data[i + 1], b: img.data[i + 2], a: img.data[i + 3] } - const processed = fn(pixel, q) - img.data[i] = processed[0] - img.data[i + 1] = processed[1] - img.data[i + 2] = processed[2] - img.data[i + 3] = processed[3] - } - ronin.surface.context.putImageData(img, 0, 0) - return rect - } - - this.saturation = (pixel, q = 1) => { - const color = 0.2126 * pixel.r + 0.7152 * pixel.g + 0.0722 * pixel.b - return [(color * (1 - q)) + (pixel.r * q), (color * (1 - q)) + (pixel.g * q), (color * (1 - q)) + (pixel.b * q), pixel.a] - } - - this.contrast = (pixel, q = 1) => { - const intercept = 128 * (1 - q) - return [pixel.r * q + intercept, pixel.g * q + intercept, pixel.b * q + intercept, pixel.a] - } - // File System this.dir = (path = this.dirpath()) => { // Returns the content of a directory. diff --git a/desktop/sources/scripts/ronin.js b/desktop/sources/scripts/ronin.js index fade53f..927d388 100644 --- a/desktop/sources/scripts/ronin.js +++ b/desktop/sources/scripts/ronin.js @@ -10,7 +10,7 @@ function Ronin () { b_low: '#aaa', b_inv: '#ffb545' } - + this.includes = ['prelude'] this.el = document.createElement('div') diff --git a/examples/basics.lisp b/examples/basics.lisp index 997be5d..3c6f391 100644 --- a/examples/basics.lisp +++ b/examples/basics.lisp @@ -6,4 +6,11 @@ ; define a function (defn add-two (a) (add 2 a)) -(echo (add-two 4)) \ No newline at end of file +(echo (add-two 4)) + +; run +(times 5 + (lambda + (a) + (echo + (concat "time:" a)))) \ No newline at end of file