You seem to be overcomplicating this. You should focus less on code and more on setting things up in the editor. A lot of code can be offloaded to the node setup. Here’s a complete example of the logic, if I understood it correctly. It’s all in one script. To run it just create tscn and script files and run the scene. Grabber and marker are made with built in gradient textures. Note that I didn’t implement the ending conditions so it will crash on the last pixel 
script:
extends HSlider
var time := 0.0
var speed := 0.0
@export var oscillation_speed := 5.0
var pixel := 0
var original: Image
var stolen: Image
func _ready():
# create random original image - load instead
original = Image.create_empty(8, 8, false, Image.FORMAT_RGB8)
for i in 64:
original.set_pixel(i % 8, i / 8, Color(randf(), randf(), randf()))
%original.texture = ImageTexture.create_from_image(original)
# create stolen image
stolen = Image.create_empty(8, 8, false, Image.FORMAT_RGB8)
%stolen.texture = ImageTexture.create_from_image(stolen)
%button.text = "START"
func _process(delta):
# animate grabber position
ratio = sin(time) * 0.5 + 0.5
$grabber.position = size * Vector2(ratio, 0.5)
time += speed * delta
# sample gradient and assign color to grabber
var g: Gradient = %slider_gradient.texture.gradient
$grabber.modulate = g.sample(ratio)
# position and animate current pixel marker
%pixel_marker.global_position = %original.global_position + Vector2(pixel % 8, pixel / 8) * %original.size / 8 + Vector2.ONE * %original.size / 16
%pixel_marker.scale = Vector2.ONE * (1.0 + sin(time * 5.0) * 0.3)
func _on_button_pressed():
if is_zero_approx(speed):
speed = 5.0
%button.text = "STOP"
set_colors_from_current_pixel()
else:
if %button.text != "START":
set_stolen_color()
pixel += 1
%button.text = "NEXT"
speed = 0.0
func set_colors_from_current_pixel():
var original_color := original.get_pixel(pixel % 8, pixel / 8)
%slider_gradient.texture.gradient.set_color(0, Color(randf(), randf(), randf()))
%slider_gradient.texture.gradient.set_color(1, original_color)
%slider_gradient.texture.gradient.set_color(2, Color(randf(), randf(), randf()))
func set_stolen_color():
stolen.set_pixel(pixel % 8, pixel / 8, $grabber.modulate)
%stolen.texture = ImageTexture.create_from_image(stolen)
tscn:
[gd_scene format=3 uid="uid://b13af8kdshws3"]
[ext_resource type="Script" uid="uid://dprkadmmcv6fh" path="res://slider.gd" id="1_7dq2a"]
[sub_resource type="Gradient" id="Gradient_sw1by"]
interpolation_mode = 1
offsets = PackedFloat32Array(0, 0.71644294)
colors = PackedColorArray(1, 1, 1, 0, 0, 0, 0, 1)
[sub_resource type="GradientTexture2D" id="GradientTexture2D_70n7g"]
gradient = SubResource("Gradient_sw1by")
fill = 2
fill_from = Vector2(0.5, 0.5)
[sub_resource type="Gradient" id="Gradient_7dq2a"]
interpolation_color_space = 1
offsets = PackedFloat32Array(0, 0.5, 1)
colors = PackedColorArray(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
[sub_resource type="GradientTexture1D" id="GradientTexture1D_sw1by"]
gradient = SubResource("Gradient_7dq2a")
[sub_resource type="Gradient" id="Gradient_70n7g"]
interpolation_mode = 1
offsets = PackedFloat32Array(0, 0.51510066, 0.61577183)
colors = PackedColorArray(1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0)
[sub_resource type="GradientTexture2D" id="GradientTexture2D_eheoe"]
gradient = SubResource("Gradient_70n7g")
width = 128
height = 128
fill = 1
fill_from = Vector2(0.5, 0.5)
[node name="slider_game" type="Control" unique_id=283951799]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="original" type="TextureRect" parent="." unique_id=96910540]
unique_name_in_owner = true
texture_filter = 1
layout_mode = 1
offset_left = 32.0
offset_top = 32.0
offset_right = 332.0
offset_bottom = 332.0
expand_mode = 1
[node name="pixel_marker" type="Sprite2D" parent="original" unique_id=395273112]
unique_name_in_owner = true
texture = SubResource("GradientTexture2D_70n7g")
[node name="stolen" type="TextureRect" parent="." unique_id=203063546]
unique_name_in_owner = true
texture_filter = 1
layout_mode = 1
anchors_preset = 1
anchor_left = 1.0
anchor_right = 1.0
offset_left = -332.0
offset_top = 32.0
offset_right = -32.0
offset_bottom = 332.0
grow_horizontal = 0
[node name="slider" type="HSlider" parent="." unique_id=42760101]
layout_mode = 1
anchors_preset = 7
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
offset_left = -271.0
offset_top = -216.0
offset_right = 271.0
offset_bottom = -149.0
grow_horizontal = 2
grow_vertical = 0
mouse_filter = 2
script = ExtResource("1_7dq2a")
[node name="slider_gradient" type="TextureRect" parent="slider" unique_id=267634757]
unique_name_in_owner = true
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
texture = SubResource("GradientTexture1D_sw1by")
[node name="grabber" type="Sprite2D" parent="slider" unique_id=1318100658]
unique_name_in_owner = true
texture = SubResource("GradientTexture2D_eheoe")
[node name="button" type="Button" parent="." unique_id=861567750]
unique_name_in_owner = true
layout_mode = 1
anchors_preset = 7
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
offset_left = -50.58069
offset_top = -104.0
offset_right = 50.58069
offset_bottom = -40.0
grow_horizontal = 2
grow_vertical = 0
action_mode = 0
text = "STOP"
[connection signal="pressed" from="button" to="slider" method="_on_button_pressed"]