#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use Tk::Font;
use File::Temp qw(tempfile);
use Data::Dumper;
use diagnostics;
use Image::Imlib2;
use Math::Round;
use feature "say";
my $save_dir="palettes/";
my @range_list=qw(light mid dark average light_mid);
my $range=$range_list[0];
my $c=0;
my @points;
my $image = Image::Imlib2->new_transparent(16, 16);
$image->image_set_format("png");
# Create main window
my $mw = MainWindow->new;
$mw->title("MAKE A PALETTE");
$mw->geometry("600x800+400+800");
my $font = $mw->Font(
    -family => 'Helvetica',
    -size   => 18,
    -weight => 'normal'
);
# weight must be "normal, bold, medium, book, light, demi, or demibold "
# Create canvas to display color
my $canvas = $mw->Canvas(-width => 200, -height => 200)->grid(qw/ -row 0 -column 0 -sticky w/);

# Variables to store slider values
my $red_s=0;
my $green_s=0;
my $blue_s=0;
my $red = 0;
my $green = 0;
my $blue = 0;
my $r_frac=0;
my $g_frac=0;
my $b_frac=0;
# Tk Photo object to display the image
my $photo = $mw->Photo(-width => 200, -height => 200);

# Attach the photo to the canvas
$canvas->createImage(100, 100, -image => $photo);

# Subroutine to update the color
sub update_color {
$red=$red_s;
$green=$green_s;
$blue=$blue_s;
    # Create PPM header
    my $ppm_data = "P6\n200 200\n255\n";

    # Add pixel data
    for my $y (0 .. 199) {
        for my $x (0 .. 199) {
            $ppm_data .= pack('C3', $red, $green, $blue);  # RGB values
        }
    }
    # Create a temporary file
    my ($fh, $filename) = tempfile(SUFFIX => '.ppm');
    print $fh $ppm_data;
    close $fh;

    # Configure the Tk Photo object with the PPM file
    $photo->read($filename);
}

# Create sliders
my $entry = $mw->Label(-font =>'{Helvetica} 10' , -text => "\n", -anchor => 'w', -width =>15)->grid(qw/ -row 8 -column 0 -sticky w/);
my $red_slider = $mw->Scale(-label => 'Red', -from => 0, -to => 255, -orient => 'horizontal', 
    -label     => 'Red',
    -from      => 0,
    -to        => 255,
    -orient    => 'horizontal',
    -variable  => \$red_s,
    -command   => sub { update_color() },
    -length    => 300,
    -font      => $font
)->grid(qw/ -row 10 -column 0 -sticky w/);

my $green_slider = $mw->Scale(-label => 'Red', -from => 0, -to => 255, -orient => 'horizontal', 
    -label     => 'Green',
    -from      => 0,
    -to        => 255,
    -orient    => 'horizontal',
    -variable  => \$green_s,
    -command   => sub { update_color() },
    -length    => 300,
    -font      => $font
)->grid(qw/ -row 11 -column 0 -sticky w/);
my $blue_slider = $mw->Scale(-label => 'Blue', -from => 0, -to => 255, -orient => 'horizontal', 
    -label     => 'Blue',
    -from      => 0,
    -to        => 255,
    -orient    => 'horizontal',
    -variable  => \$blue_s,
    -command   => sub { update_color() },
    -length    => 300,
    -font      => $font
)->grid(qw/ -row 12 -column 0 -sticky w/);


$entry = $mw->Label(-font => $font , -text => "\n", -anchor => 'w', -width =>15)->grid(qw/ -row 14 -column 0 -sticky w/);

$entry = $mw->Label(-font => $font, -text => "brightness", -anchor => 'w', -width =>15)->grid(qw/ -row 16 -column 0 -sticky w/);
$entry = $mw->Entry(-font => $font,  -width =>5, -textvariable => \$range)->grid(qw/ -row 16 -column 1 -sticky w/);
my $button=$mw->Button(-font => $font, -text => "->", -width => 2, -command => sub {$c=$c+1;if ($c>4){$c=0};$range=$range_list[$c];})->grid(qw/ -row 16 -column 2 -sticky w/);

$button=$mw->Button(-font => $font, -text => "MAKE", -justify => 'center',  -relief =>'raised', -borderwidth => 2, -pady => 2, -padx  => 2, -width => 8, -command => sub {&palette()})->grid(qw/ -row 20 -column 0 -sticky w/);;
$button=$mw->Button(-font => $font, -width =>8, -text => "CLOSE", -justify => 'center', -relief =>'raised', -borderwidth => 2, -pady => 2, -padx  => 2, -command =>sub{$mw->destroy;})->grid(qw/ -row 22 -column 0 -sticky w/);

update_color();

MainLoop;


sub palette{
say "colours $red $green $blue";
my ($small, $mid, $big) = sort { $a <=> $b } ($red_s, $green_s, $blue_s);
my ($small_color, $mid_color, $big_color);
if ($red_s == $small) {
    $small_color = 'red';
    if ($green_s == $mid) {
        $mid_color = 'green';
        $big_color = 'blue';
    } else {
        $mid_color = 'blue';
        $big_color = 'green';
    }
} elsif ($green_s == $small) {
    $small_color = 'green';
    if ($red_s == $mid) {
        $mid_color = 'red';
        $big_color = 'blue';
    } else {
        $mid_color = 'blue';
        $big_color = 'red';
    }
} else {
    $small_color = 'blue';
    if ($red_s == $mid) {
        $mid_color = 'red';
        $big_color = 'green';
    } else {
        $mid_color = 'green';
        $big_color = 'red';
    }
}

# Output the results
say "Initial values: red = $red_s, green = $green_s, blue = $blue_s";
say "Sorted values: small = $small ($small_color), mid = $mid ($mid_color), big = $big ($big_color)";

################
# Small Line: y = x
for my $x (0 .. 255) {
    my $y = $x;
    $canvas->createLine($x, $y, $x, $y, -fill => 'blue');  # Change color if desired
}

# Mid Line using quadratic Bezier curve approximation
for my $x (0 .. 255) {
    my $t = $x / 255;
    my $y = (1 - $t) ** 2 * 40 + 2 * (1 - $t) * $t * 215 + $t ** 2 * 255;
    $canvas->createLine($x, $y, $x, $y, -fill => 'red');  # Change color if desired
}

# Big Line: y = 100 + (155/255) * sqrt(255^2 - x^2)
for my $x (0 .. 255) {
    my $y = 100 + (155 / 255) * sqrt(255**2 - $x**2);
    $canvas->createLine($x, $y, $x, $y, -fill => 'green');  # Change color if desired
}

##############
# Subroutines for generating values
sub generate_big_value {
    my ($x, $color_value) = @_;
    # Replace with the actual equation for big using $color_value
    return $color_value * ($x / 255);  # Placeholder equation
}

sub generate_mid_value {
    my ($x, $color_value) = @_;
    # Replace with the actual equation for mid using $color_value
    return $color_value * 0.8 + 40 * ($x / 255);  # Placeholder equation
}

sub generate_small_value {
    my ($x, $color_value) = @_;
    # Replace with the actual equation for small using $color_value
    return sqrt(255**2 - $x**2) * 0.6 + 100 * ($color_value / 255);  # Placeholder equation
}
##############
# Use the subroutines with the correct values
for my $x (0 .. 255) {
    my $big_value = generate_big_value($x, $big);
    my $mid_value = generate_mid_value($x, $mid);
    my $small_value = generate_small_value($x, $small);

    # Output the generated values
 #   say "x = $x: big = $big_value ($big_color), mid = $mid_value ($mid_color), small = $small_value ($small_color)";
say " big_value = $big_value big colour = $big_color";;
}
############
#say " big_value = $big_value big colour = $big_colour";;

return;

# Small Line: y = x
for my $x (0 .. 255) {
    my $y = $x;
    $canvas->createLine($x, $y, $x, $y, -fill => 'blue');  # Change color if desired
}

# Mid Line using quadratic Bezier curve approximation
for my $x (0 .. 255) {
    my $t = $x / 255;
    my $y = (1 - $t) ** 2 * 40 + 2 * (1 - $t) * $t * 215 + $t ** 2 * 255;
    $canvas->createLine($x, $y, $x, $y, -fill => 'red');  # Change color if desired
}

# Big Line: y = 100 + (155/255) * sqrt(255^2 - x^2)
for my $x (0 .. 255) {
    my $y = 100 + (155 / 255) * sqrt(255**2 - $x**2);
    $canvas->createLine($x, $y, $x, $y, -fill => 'green');  # Change color if desired
}

###########################













say "frac-rgb = $r_frac $g_frac $b_frac";
my $pixel=0;
for (my $i=0;$i<=14;$i++){
for (my $t=0;$t<=15;$t++){
$red=($pixel*$r_frac);
$green=($pixel*$g_frac);
$blue=($pixel*$b_frac);
if($red<0){$red=0}
if($red>255){$red=255}
if($green<0){$green=0}
if($green>255){$green=255}
if($blue<0){$blue=0}
if($blue>255){$blue=255}

$points[$i][$t]{red}=$red;
$points[$i][$t]{green}=$green;
$points[$i][$t]{blue}=$blue;
$pixel++;
}
}

for (my $t=0;$t<=14;$t++){
$red=($pixel*$r_frac);
$green=($pixel*$g_frac);
$blue=($pixel*$b_frac);

if($red<0){$red=0}
if($red>255){$red=255}
if($green<0){$green=0}
if($green>255){$green=255}
if($blue<0){$blue=0}
if($blue>255){$blue=255}
$points[15][$t]{red}=$red;	 
$points[15][$t]{green}=$green;
$points[15][$t]{blue}=$blue;
}
#it's the order RGB for Imlib2 decides if it's R, G or B
#Save the array of pixels as an image
for (my $i=0;$i<=14;$i++){
for (my $t=0;$t<=15;$t++){
$points[$i][$t]{red}=round($points[$i][$t]{red});
$points[$i][$t]{blue}=round($points[$i][$t]{blue});
$points[$i][$t]{green}=round($points[$i][$t]{green});
$image->set_colour($points[$i][$t]{red},$points[$i][$t]{green},$points[$i][$t]{blue},255);
$image->draw_point($t,$i);
}
say "red:$points[$i][0]{red} blue:$points[$i][0]{blue} green:$points[$i][0]{green}";
}
for (my $t=0;$t<=14;$t++){
$image->set_colour($points[15][$t]{red},$points[15][$t]{green},$points[15][$t]{blue},255);
$image->draw_point($t,15);
}
say "redS = $red_s blueS = $blue_s greenS = $green_s";
say "\n";

my $save_name="$red_s:$green_s:$blue_s.png";
my $save_file="palettes/$range:$save_name";
$image->save("$save_file");

#End save the array of pixels as an image


}#end sub	
