Saving color settings
Posted: Sun Sep 11, 2016 10:09 am
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.
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