about summary refs log tree commit diff stats
path: root/505colors.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-07-06 08:39:46 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-07-06 08:39:46 -0700
commitc31c02f0d11511fd8e3e2fe934dd1609ea97eb69 (patch)
tree69f2be4b59f99ca9cf2e0373e893cae6f6953a5a /505colors.mu
parentd84d17d6f5c3f7f3c7718bf42957d61ba4135609 (diff)
downloadmu-c31c02f0d11511fd8e3e2fe934dd1609ea97eb69.tar.gz
.
Diffstat (limited to '505colors.mu')
-rw-r--r--505colors.mu44
1 files changed, 44 insertions, 0 deletions
diff --git a/505colors.mu b/505colors.mu
index 3185f9f7..3d8d28f1 100644
--- a/505colors.mu
+++ b/505colors.mu
@@ -211,6 +211,50 @@ fn test-hsl-cyan {
   check-ints-equal l, 0x7f, "F - test-hsl-cyan/luminance"  # TODO: should round up
 }
 
+fn nearest-color-euclidean-hsl h: int, s: int, l: int -> _/eax: int {
+  var result/edi: int <- copy 0x100/invalid
+  var max-distance/esi: int <- copy 0x30000/max  # 3 * 0x100*0x100
+  var a/ecx: int <- copy 0
+  var b/edx: int <- copy 0
+  var c/ebx: int <- copy 0
+  var color/eax: int <- copy 0
+  {
+    compare color, 0x100
+    break-if->=
+    $nearest-color-euclidean-hsl:body: {
+      a, b, c <- color-rgb color
+      a, b, c <- hsl a, b, c
+      {
+        var curr-distance/eax: int <- euclidean-distance-squared a, b, c, h, s, l
+        compare curr-distance, max-distance
+        break-if->= $nearest-color-euclidean-hsl:body
+        max-distance <- copy curr-distance
+      }
+      result <- copy color
+    }
+    color <- increment
+    loop
+  }
+  return result
+}
+
+fn euclidean-distance-squared x1: int, y1: int, z1: int, x2: int, y2: int, z2: int -> _/eax: int {
+  var result/edi: int <- copy 0
+  var tmp/eax: int <- copy x1
+  tmp <- subtract x2
+  tmp <- multiply tmp
+  result <- add tmp
+  tmp <- copy y1
+  tmp <- subtract y2
+  tmp <- multiply tmp
+  result <- add tmp
+  tmp <- copy z1
+  tmp <- subtract z2
+  tmp <- multiply tmp
+  result <- add tmp
+  return result
+}
+
 ###
 
 fn maximum a: int, b: int -> _/eax: int {