-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.bas
51 lines (44 loc) · 1.65 KB
/
test.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
' Load the image
imageHandle = _LOADIMAGE("path/to/image.png")
' Define the EGA palette (example with 4 colors for simplicity)
DIM egaPalette(3) AS _UNSIGNED LONG
egaPalette(0) = _RGB(0, 0, 0) ' Black
egaPalette(1) = _RGB(0, 0, 170) ' Blue
egaPalette(2) = _RGB(0, 170, 0) ' Green
egaPalette(3) = _RGB(0, 170, 170) ' Cyan
' Function to find the closest color in the palette
FUNCTION FindClosestColor (color AS _UNSIGNED LONG)
DIM minDistance AS DOUBLE
DIM closestColor AS _UNSIGNED LONG
minDistance = 1E+30 ' Initialize with a large number
FOR i = 0 TO UBOUND(egaPalette)
distance = ColorDistance(color, egaPalette(i))
IF distance < minDistance THEN
minDistance = distance
closestColor = egaPalette(i)
END IF
NEXT i
FindClosestColor = closestColor
END FUNCTION
' Function to calculate the distance between two colors
FUNCTION ColorDistance (color1 AS _UNSIGNED LONG, color2 AS _UNSIGNED LONG)
r1 = _RED(color1)
g1 = _GREEN(color1)
b1 = _BLUE(color1)
r2 = _RED(color2)
g2 = _GREEN(color2)
b2 = _BLUE(color2)
ColorDistance = SQR((r2 - r1) ^ 2 + (g2 - g1) ^ 2 + (b2 - b1) ^ 2)
END FUNCTION
' Create a new image with the palette colors
newImageHandle = _NEWIMAGE(_WIDTH(imageHandle), _HEIGHT(imageHandle), 32)
FOR y = 0 TO _HEIGHT(imageHandle) - 1
FOR x = 0 TO _WIDTH(imageHandle) - 1
originalColor = POINT(x, y, imageHandle)
newColor = FindClosestColor(originalColor)
PSET (x, y), newColor, newImageHandle
NEXT x
NEXT y
' Display the new image
SCREEN _NEWIMAGE(_WIDTH(imageHandle), _HEIGHT(imageHandle), 32)
_PUTIMAGE (0, 0), newImageHandle