From fe903c535211bdbeeb703e06db0da3f7c8c19b4b Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Tue, 16 Sep 2025 01:33:43 -0400 Subject: Update --- src/wasp-utils.el | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'src/wasp-utils.el') diff --git a/src/wasp-utils.el b/src/wasp-utils.el index cc9573f1..894c62f3 100644 --- a/src/wasp-utils.el +++ b/src/wasp-utils.el @@ -1,4 +1,4 @@ -;;; wasp-utils --- Miscellaneous utilities -*- lexical-binding: t; -*- +;;; wasp-utils --- Miscellaneous utilities -*- lexical-binding: t; byte-compile-warnings: (not suspicious); -*- ;;; Commentary: ;;; Code: @@ -235,5 +235,50 @@ If TEXT is nil, use the empty string instead." :weight 'bold :extend t))) +(defun w/random-color () + "Return a random color string." + (let ( (r (random 256)) + (g (random 256)) + (b (random 256))) + (format "#%02x%02x%02x" r g b))) + +(defun w/aref-u32-be (a idx) + "Read a big-endian 32-bit integer starting at IDX from A." + (logior + (lsh (aref a idx) 24) + (lsh (aref a (+ idx 1)) 16) + (lsh (aref a (+ idx 2)) 8) + (aref a (+ idx 3)))) + +(defun w/aref-u16-be (a idx) + "Read a big-endian 16-bit integer starting at IDX from A." + (logior + (lsh (aref a idx) 8) + (aref a (+ idx 1)))) + +(defun w/load-image-ff (path) + "Load the Farbfeld image at PATH. +Return a list of the width, height, and pixels of the image." + (when-let* + ((data (f-read-bytes path)) + ((s-prefix? "farbfeld" data)) + (width (w/aref-u32-be data 8)) + (height (w/aref-u32-be data 12)) + (pixels + (--map + (let ((a (+ 16 (* it 8)))) + (list + (lsh (w/aref-u16-be data a) -8) + (lsh (w/aref-u16-be data (+ a 2)) -8) + (lsh (w/aref-u16-be data (+ a 4)) -8))) + (-iota (* width height))))) + (list width height (seq-into pixels 'vector)))) + +(defun w/load-image-png (path) + "Load the PNG image at PATH (by converting to Farbfeld first)." + (let ((tmp "/tmp/udcff.ff")) + (when (= 0 (call-process-shell-command (format "png2ff <'%s' >'%s'" path tmp) nil "*udc-png-error*")) + (w/load-image-ff tmp)))) + (provide 'wasp-utils) ;;; wasp-utils.el ends here -- cgit v1.2.3