This commit is contained in:
visionmercer 2026-05-20 22:38:36 +02:00
commit 41b28e4fd2
2 changed files with 71 additions and 5 deletions

View file

@ -222,3 +222,71 @@ sub floodfill (startx,starty,fillcolor~&)
wend
end sub
sub 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)
dim fb as integer:fb=_blue(fcol)
dim br as integer:br=_red(bcol)
dim bg as integer:bg=_green(bcol)
dim bb as integer:bb=_blue(bcol)
' 4. 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
' 5. Loop through every single pixel on the canvas layer
dim x as long,y as long
dim t as single,threshold as single
dim targetr as integer,targetg as integer,targetb as integer
dim mixedcolor as _unsigned long
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
' Get the ordered dither adjustment ratio (-0.5 to +0.5 range influence)
threshold=(bayer(x mod 4,y mod 4)/16.0)-0.5
' Apply blend factor altered by our dither threshold noise
dim blend as single:blend=t+threshold
if blend<0 then blend=0
if blend>1 then blend=1
' Linearly interpolate colors
targetr=fr+(br-fr)*blend
targetg=fg+(bg-fg)*blend
targetb=fb+(bb-fb)*blend
' Find the absolute closest matching color available in your specific palette
mixedcolor=closestcolor(_rgb32(targetr,targetg,targetb),pal())
' Draw directly onto the destination image
pset (x,y),mixedcolor
next x
next y
end sub