variable gets overwritten in operator

Hi. I’ve run into a little problem that almost looks like it’s breaking the coding rules.

The script is a basic one that creates variation every time you create a stroke in the image editor. The idea would be that you can have sliders that can place variation on hue, saturation and value independently. Each brush stroke would be a variation of the initial color, while going back to the initial color every time you let go.

So I created an operator that looks for an event where the mouse button is pushed down. When the button is NOT pushed down then the current color in memory(brush_col). When it’s pushed down the color changes and the brush paints with the new color. Once you let go of the button it changes the color back to the old color and then goes back to the beginning ("…places the current color in memory(brush_col)").

Only problem is that it doesn’t want to change the color back to the initial color. I can get it to work(the brush goes back to initial color when released) by taking out the line(but then I can’t change the color via color wheel):
"

if i delete the line below the scipt works, but I can’t choose a new color

                brush_col = bpy.context.tool_settings.image_paint.brush.color;


"

This in theory shouldn’t change anything as this line doesn’t run unless I change the color.

Sorry if this is confusing. Maybe run the script and paint in the Image Editor and have a look at the console for what is happening.

import bpy
from bpy.props import IntProperty, FloatProperty
from random import randint


#brush_col = None;# = bpy.context.tool_settings.image_paint.brush.color;
brush_col = (1,0.5,1);
rand_col = (1,1,1);


class ModalOperator(bpy.types.Operator):
    """Paint random color stroke"""
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"


    first_mouse_x = IntProperty()
    first_value = FloatProperty()
    
    def modal(self, context, event):
            
        global brush_col
        global rand_col
        
        
        
        if event.type == 'LEFTMOUSE':
            if event.value == 'PRESS':
                
                print("PRESSING LEFT MOUSE");
                
                print(brush_col);
                
                #### create random color to paint with
                rand_col = ((brush_col[0] - (randint(0, 100)/500), brush_col[1] - (randint(0, 100)/500), brush_col[2] - (randint(0, 100)/500)));
                bpy.context.tool_settings.image_paint.brush.color = rand_col;#((brush_col[0] - (randint(0, 100)/500), brush_col[1] - (randint(0, 100)/500), brush_col[2] - (randint(0, 100)/500)));
                
                


        if event.value == 'RELEASE':


            print("RELAESING LEFT MOUSE"); 
            
            cur_col = bpy.context.tool_settings.image_paint.brush.color;
            cur_brush_col = bpy.data.brushes["TexDraw"].color;
            
            #### if current col != random col
            #### user must have changed color
            if (round(cur_col[0],3) != round(rand_col[0],3)): 
                    print (">User changed color...");
                    #print (round(rand_col[0],3));
                    #print (round(cur_col[0],3));
                    #print (rand_col);
                    #print (cur_col);
                    #### if i delete the line below the scipt works, but I can't choose a new color
                    brush_col = bpy.context.tool_settings.image_paint.brush.color;


            #### change color back to initial color
            bpy.context.tool_settings.image_paint.brush.color = brush_col;
            


        elif event.type in {'RIGHTMOUSE', 'ESC'}:
            context.object.location.x = self.first_value
            return {'CANCELLED'}


        return {'PASS_THROUGH'}


    def invoke(self, context, event):
        if context.object:
            self.first_mouse_x = event.mouse_x
            self.first_value = context.object.location.x


            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, "No active object, could not finish")
            return {'CANCELLED'}




def register():
    bpy.utils.register_class(ModalOperator)




def unregister():
    bpy.utils.unregister_class(ModalOperator)




if __name__ == "__main__":
    register()


    # test call
    bpy.ops.object.modal_operator('INVOKE_DEFAULT')