Saving color settings

Post Reply
Henko
Posts: 816
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Saving color settings

Post by Henko »

Two functions which may be used to save an actual color setting and restore the setting later on. Typically, a function might use its own drawing color, and should restore the correspondent setting in the calling program (function). These functions perform this for the drawing color. Likewise functions could be made fore other types of setting.
As functions may be nested, the save/restore functions are stack oriented. The max number of saved settings is 10, but can be changed as necessary.
The functions are independent with regard to the "option base 0/1" command.
The functions return 1 in case of succesfull stack operation, and 0 in case of attempted stack underflow/overflow.
Functions using the save/restore facility do not need more than the two function calls:

Def func (......)
save_draw_color()
..
function body (may include other calls to functions wich specific color)
..
restore_draw_color()
return ....
end def

There is one pecularity: pixel 0,0 is used to determine the current color (i.e.: the pixel is drawn using the current setting and then read). I did not bother to restore the original background of pixel 0,0 after using it.

Code: Select all

def save_draw_color()
max_sp=10              ' max stack depth = 10
dim col(max_sp+1,5)
if stack_pointer=max_sp then return 0 else stack_pointer+=1
draw pixel 0,0 ! get pixel 0,0 color r,g,b,alfa
col(stack_pointer,1)=r
col(stack_pointer,2)=g
col(stack_pointer,3)=b
col(stack_pointer,4)=alfa
return 1
end def

def restore_draw_color()
if save_draw_color.stack_pointer = 0 then return 0
r=save_draw_color.col(save_draw_color.stack_pointer,1)
g=save_draw_color.col(save_draw_color.stack_pointer,2)
b=save_draw_color.col(save_draw_color.stack_pointer,3)
a=save_draw_color.col(save_draw_color.stack_pointer,4)
draw color r,g,b ! draw alpha a
save_draw_color.stack_pointer-=1
return 1
end def

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Saving color settings

Post by rbytes »

Useful functions, thanks.

I wondered if DRAW PIXEL and GET PIXEL might work outside the screen boundaries, the way other draw commands do. That would ensure that the user would never see the pixel - just in case it was high contrast, such as black on white or white on black. So I tried changing the DRAW PIXEL and GET PIXEL location to -1,0. It didn't work.

The commands work on my first call, but I can't seem to store and recall more than one draw color setting. Would you please post example code for saving and recalling more than one draw color?
The only thing that gets me down is gravity...

Henko
Posts: 816
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Saving color settings

Post by Henko »

ricardobytes wrote:Useful functions, thanks.

I wondered if DRAW PIXEL and GET PIXEL might work outside the screen boundaries, the way other draw commands do. That would ensure that the user would never see the pixel - just in case it was high contrast, such as black on white or white on black. So I tried changing the DRAW PIXEL and GET PIXEL location to -1,0. It didn't work.

The commands work on my first call, but I can't seem to store and recall more than one draw color setting. Would you please post example code for saving and recalling more than one draw color?
You got me! The DIM statement in the "save_draw_color" function must be removed, as it clears the stack array each time. By just removing it, the stack depth is limited to 9. If larger depth is needed, then a global stack array should be defined and the code adapted. In the following code, a nested function call is incorporated, and the original draw color is restored correctly after that. Thanks for testing! :|

Code: Select all

graphics ! graphics clear ! draw color .4,.4,.4 ! draw size 2
draw text "this is the original color" at 10,10
put_text(10,40,"this text in red",1,0,0)
draw text "and this should be the original color" at 10,120
end

def put_text(x,y,t$,r,g,b)
save_draw_color()
draw color r,g,b
draw text t$ at x,y
put_text1(2*x,2*y,"this text in blue",0,0,1)  ' nested function call
restore_draw_color()
end def

def put_text1(x,y,t$,r,g,b)
save_draw_color()
draw color r,g,b
draw text t$ at x,y
restore_draw_color()
end def

def save_draw_color()
max_sp=9              ' max stack depth = 9
if stack_pointer=max_sp then return 0 else stack_pointer+=1
draw pixel 0,0 ! get pixel 0,0 color r,g,b,alfa
col(stack_pointer,1)=r
col(stack_pointer,2)=g
col(stack_pointer,3)=b
col(stack_pointer,4)=alfa
return 1
end def

def restore_draw_color()
if save_draw_color.stack_pointer = 0 then return 0
r=save_draw_color.col(save_draw_color.stack_pointer,1)
g=save_draw_color.col(save_draw_color.stack_pointer,2)
b=save_draw_color.col(save_draw_color.stack_pointer,3)
a=save_draw_color.col(save_draw_color.stack_pointer,4)
draw color r,g,b ! draw alpha a
save_draw_color.stack_pointer-=1
return 1
end def

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Saving color settings

Post by rbytes »

You could also just change the DIM line to
IF NOT stack_pointer THEN DIM col(max_sp+1,5)

That will ensure that the array only gets dimensioned once, on the first call to the function.

These functions are powerful, since with a few changes they can be used to restore virtually anything - font name, font size, font color, and on and on.

I will certainly make use of them. Thanks for posting! :arrow: :idea:
The only thing that gets me down is gravity...

Henko
Posts: 816
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Saving color settings

Post by Henko »

Another way to demonstrate, using recursion. Your conditional DIM statement works fine.

Code: Select all

graphics ! graphics clear ! draw color .4,.4,.4 ! draw size 2
draw text "this is the original color in main()" at 10,10
func(8)
draw text "and finally back in main()" at 10,300
end

def func(i)
save_col()
pause .5
draw color rnd(.6),rnd(.6),rnd(.6)
n+=1
draw text "call " & n & " to function" at 30*n,30*n+10
i-=1
if i then func(i)  ' recursive call
pause .5
draw text "and back" at 30*n+300,30*n+10
restore_col()
end def

def save_col()
max_sp=10              ' max stack depth = 10
if sp=0 then dim col(max_sp+1,5)
if sp=max_sp then return 0 else sp+=1
draw pixel 0,0 ! get pixel 0,0 color r,g,b,alfa
col(sp,1)=r ! col(sp,2)=g ! col(sp,3)=b ! col(sp,4)=alfa
return 1
end def

def restore_col()
if save_col.sp = 0 then return 0
r=save_col.col(save_col.sp,1) ! g=save_col.col(save_col.sp,2)
b=save_col.col(save_col.sp,3) ! a=save_col.col(save_col.sp,4)
draw color r,g,b ! draw alpha a
save_col.sp-=1
return 1
end def
image.jpeg
image.jpeg (325.6 KiB) Viewed 1799 times

Post Reply