Der Code berechnet im Grunde einfach den euklidischen Abstand zwischen allen 16 DOS-Farben und der gesuchten Farbe, die man als 24-Bit-Code angegeben hat. Und dann kriegt man als Ergebnis die Nummer der Farbe, die am nächsten an der gesuchten Farbe dran liegt.
Edit:
Hier noch eine abgeänderte Version, die auch direkt lauffähig ist:
Code: Alles auswählen
Structure Color24
r.a
g.a
b.a
EndStructure
Structure Color24Array
count.i
c.Color24[0]
EndStructure
Global *colorPalette16.Color24Array = ?ColorPalette16
Procedure getNearestPaletteColor(color.l, *palette.Color24Array)
Protected i.i, min.i = 255 * 255 * 3, diff.i, rDiff.i, gDiff.i, bDiff.i
Protected minColor.i = -1
For i = 0 To *palette\count - 1
rDiff = Red(color) - *palette\c[i]\r
gDiff = Green(color) - *palette\c[i]\g
bDiff = Blue(color) - *palette\c[i]\b
diff = rDiff * rDiff + gDiff * gDiff + bDiff * bDiff
If (diff < min)
minColor = i
min = diff
EndIf
Next
ProcedureReturn minColor
EndProcedure
Debug getNearestPaletteColor(RGB(12, 34, 56), *colorPalette16)
Debug getNearestPaletteColor(RGB(127, 0, 200), *colorPalette16)
DataSection
ColorPalette16:
Data.i 16
Data.a 0, 0, 0,
0, 0, 127,
0, 127, 0,
0, 127, 127,
127, 0, 0,
127, 0, 127,
127, 127, 0,
191, 191, 191,
127, 127, 127,
0, 0, 255,
0, 255, 0,
0, 255, 255,
255, 0, 0,
255, 0, 255,
255, 255, 0,
255, 255, 255
EndDataSection