different dither

This commit is contained in:
visionmercer 2026-05-21 09:22:52 +02:00
commit 89602f1804
3 changed files with 75 additions and 0 deletions

View file

@ -238,6 +238,53 @@ sub ditheredgradient (x1 as long,y1 as long,x2 as long,y2 as long,fcol as _unsig
exit sub
end if
' 3. Define a standard 4x4 Bayer Dithering Matrix (scaled 0 to 15)
dim bayer(3,3) as integer
bayer(0,0)=0:bayer(0,2)=8:bayer(0,1)=2:bayer(0,3)=10
bayer(2,0)=12:bayer(2,2)=4:bayer(2,1)=14:bayer(2,3)=6
bayer(1,0)=3:bayer(1,2)=11:bayer(1,1)=1:bayer(1,3)=9
bayer(3,0)=15:bayer(3,2)=7:bayer(3,1)=13:bayer(3,3)=5
' 4. Loop through every single pixel on the canvas layer
dim x as long,y as long
dim t as single
for y=0 to canvash-1
for x=0 to canvasw-1
' Project current pixel onto the line vector to get interpolation factor 't'
t=((x-x1)*dx+(y-y1)*dy)/linelensq
' Clamp t strictly between 0.0 and 1.0
if t<0 then t=0
if t>1 then t=1
' Compare t directly against the normalized dither matrix value.
' Adding 0.5 ensures pure fcol at t=0 and pure bcol at t=1 without edge bleeding.
if t >= (bayer(x mod 4, y mod 4) + 0.5) / 16.0 then
pset (x,y),bcol
else
pset (x,y),fcol
end if
next x
next y
end sub
sub old_ditheredgradient (x1 as long,y1 as long,x2 as long,y2 as long,fcol as _unsigned long,bcol as _unsigned long)
' 1. Get canvas boundaries from the active drawing layer
dim canvasw as integer:canvasw=_width(layers(1).ihandle)
dim canvash as integer:canvash=_height(layers(1).ihandle)
' 2. Calculate vector properties of the drawn line
dim dx as single:dx=x2-x1
dim dy as single:dy=y2-y1
dim linelensq as single:linelensq=(dx*dx)+(dy*dy)
' If the user didn't drag the mouse anywhere, just paint a solid pixel and exit
if linelensq=0 then
pset (x1,y1),fcol
exit sub
end if
' 3. Extract RGB components of Foreground and Background colors
dim fr as integer:fr=_red(fcol)
dim fg as integer:fg=_green(fcol)