This commit is contained in:
Kartik K. Agaram 2021-07-06 08:39:46 -07:00
parent d84d17d6f5
commit c31c02f0d1
2 changed files with 44 additions and 44 deletions

View File

@ -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 {

View File

@ -240,47 +240,3 @@ fn print-nearby-colors screen: (addr screen), h: int, s: int, l: int {
loop
}
}
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
}