The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Tk::Canvas::GradientColor - To create a Canvas widget with background gradient color.

SYNOPSIS

  #!/usr/bin/perl
  use strict;
  use warnings;
  
  use Tk;
  use Tk::Canvas::GradientColor;
  
  my $mw = MainWindow->new(
    -title      => 'Tk::Canvas::GradientColor',
    -background => 'white',
  );
  
  my $canvas = $mw->GradientColor(
    -width  => 400,
    -height => 400,
  )->pack(qw/ -fill both -expand 1 /);
  
  $mw->update;
  sleep 3;
  
  # Change color
  $canvas->set_gradientcolor(
    -start_color => '#000000',
    -end_color   => '#00CDFF',
  );
  
  $mw->update;
  sleep 3;
  
  # Change type
  $canvas->set_gradientcolor(
    -start       => 50,
    -end         => 100,
    -type        => 'mirror_vertical'
  );
  
  MainLoop();

DESCRIPTION

Tk::Canvas::GradientColor is an extension of the Canvas widget. It is an easy way to build a canvas widget with gradient background color.

STANDARD OPTIONS

-background -borderwidth -closeenough -confine -cursor -height -highlightbackground -highlightcolor -highlightthickness -insertbackground -insertborderwidth -insertofftime -insertontime -insertwidth -relief -scrollregion -selectbackground -selectborderwidth -selectforeground -takefocus -width -xscrollcommand -xscrollincrement -yscrollcommand -yscrollincrement

WIDGET-SPECIFIC METHODS

The Canvas method creates a widget object. This object supports the configure and cget methods described in Tk::options which can be used to enquire and modify the options described above.

disabled_gradientcolor

$canvas_bg->disabled_gradientcolor

Disabled background gradient color. The canvas widget will have the background color set by -background option.

  $canvas_bg->disabled_gradientcolor;  

enabled_gradientcolor

$canvas_bg->enabled_gradientcolor

Enabled background gradient color. Background gradient color is activated by default. Use this method if disabled_gradientcolor method is called.

  $canvas_bg->enabled_gradientcolor;  

get_gradientcolor

$canvas_bg->get_gradientcolor

Return a hash reference which contains the options to create the background gradient color.

  my $ref_gradient_options = $canvas_bg->get_gradientcolor;  

set_gradientcolor

$canvas_bg->set_gradientcolor(?options)
  • -type

    8 types are available : linear_horizontal, linear_vertical, mirror_horizontal, mirror_vertical, radial, losange, corner_right and corner_left.

        -type => 'corner_left',

    Default : linear_horizontal

  • -start_color

    First color of gradient color.

        -start_color => 'red',

    Default : #8BC2F5

  • -end_color

    Last color of gradient color.

        -end_color => '#FFCD9F',

    Default : white

  • -start

        -start => 50, # Must be >= 0, <= 100 and start < end

    Use it for linear_horizontal and linear_vertical type. The first color starts at 'start' percent width of canvas. The easy way to understand is to test the example in this documentation.

    Ex : width canvas = 1000px, start = 50 : the first part of canvas has the background color of start_color and the gradient color start at 500px.

    Default : 0

    Use it for mirror_horizontal and mirror_vertical type. The first color starts at 'start' percent width of canvas. The easy way to understand is to test the example in this documentation.

    Ex : width canvas = 1000px, start = 50 : the background gradient color begins at 50 percent in two directions.

    Default : 50

  • -end

        -end => 80,  # Must be >= 0, <= 100 and end > start

    Use it for linear_horizontal and linear_vertical type. The last color finishes at 'end' percent width of canvas. The easy way to understand is to test the example in this documentation.

    Default : 100

    Use it for mirror_horizontal and mirror_vertical type. The last color finishes at 'end' percent width of canvas and opposite. The easy way to understand is to test the example in this documentation.

    Default : 100

  • -number_color

    Number of colors between first and last color to create the gradient.

        -number_color => 200, 

    Default : 100

rgb_to_hex

$canvas_bg->rgb_to_hex($red, $green, $blue)

Return hexa code of rgb color.

  my $color = $canvas_bg->rgb_to_hex(200, 102, 65);  # return #C86641

hex_to_rgb

$canvas_bg->hex_to_rgb(string)

Return an array with red, green an blue code rgb color.

  my ( $red, $green, $blue ) = $canvas_bg->hex_to_rgb('#C86641');  # return 200, 102, 65
  my ( $red, $green, $blue ) = $canvas_bg->hex_to_rgb('gray');     # return 190, 190, 190

EXAMPLES

An example to test the configuration of the widget:

  #!/usr/bin/perl
  use strict;
  use warnings;
  
  use Tk;
  use Tk::Canvas::GradientColor;
  use Tk::BrowseEntry;
  
  my $mw = MainWindow->new(
    -title      => 'gradient color with canvas',
    -background => 'snow',
  );
  
  my $canvas = $mw->GradientColor(
    -background => '#005500',
    -width      => 500,
    -height     => 500,
  )->pack(qw/ -fill both -expand 1 /);
  
  my %arg_gradient = (
    -type         => undef,
    -start_color  => '#A780C1',
    -end_color    => 'white',
    -start        => undef,
    -end          => undef,
    -number_color => undef,
  );
  
  # configure start color
  my $bouton_color1 = $canvas->Button(
    -text    => 'select color start',
    -command => sub {
      $arg_gradient{-start_color} = $canvas->chooseColor( -title => 'select color start' );
      $canvas->set_gradientcolor(%arg_gradient);
    },
  );
  
  # configure end color
  my $bouton_color2 = $canvas->Button(
    -text    => 'select color end',
    -command => sub {
      $arg_gradient{-end_color} = $canvas->chooseColor( -title => 'select color end' );
      $canvas->set_gradientcolor(%arg_gradient);
    },
  );
  
  my $type = $canvas->BrowseEntry(
    -label   => 'Type gradient color',
    -choices => [
      qw/ linear_horizontal linear_vertical mirror_horizontal mirror_vertical radial losange corner_right corner_left/
    ],
      -background => 'white',
    -state              => 'readonly',
    -disabledbackground => 'yellow',
    -browsecmd          => sub {
      my ( $widget, $selection ) = @_;
      $arg_gradient{-type} = $selection;
      $canvas->set_gradientcolor(%arg_gradient);
    },
  );
  
  my $start_num = $canvas->Scale(
    -background   => 'white',
    -label        => 'Start',
    -from         => 0,
    -to           => 100,
    -variable     => 0,
    -orient       => 'horizontal',
    -sliderlength => 10,
    -command      => sub {
      my $selection = shift;
      $arg_gradient{-start} = $selection;
      $canvas->set_gradientcolor(%arg_gradient);
    },
  );
  
  my $end_num = $canvas->Scale(
    -background   => 'white',
    -label        => 'End',
    -from         => 0,
    -to           => 100,
    -variable     => '100',
    -orient       => 'horizontal',
    -sliderlength => 10,
    -command      => sub {
      my $selection = shift;
      $arg_gradient{-end} = $selection;
      $canvas->set_gradientcolor(%arg_gradient);
    },
  );
  my $num                = 100;
  my $entry_number_color = $canvas->BrowseEntry(
    -label              => 'Number color',
    -choices            => [qw/ 2 3 4 5 10 50 100 150 200 250 300 400 500 750 1000 1500 2000 2500/],
    -state              => 'readonly',
    -disabledbackground => 'yellow',
    -background => 'white',
    -variable           => \$num,
    -browsecmd          => sub {
      my ( $widget, $selection ) = @_;
      $arg_gradient{-number_color} = $selection;
      $canvas->set_gradientcolor(%arg_gradient);
    },
  );
  
  my $disabled_gradientcolor = $canvas->Button(
    -text    => 'disabled_gradientcolor',
    -command => sub { $canvas->disabled_gradientcolor; },
  );
  my $enabled_gradientcolor = $canvas->Button(
    -text    => 'enabled_gradientcolor',
    -command => sub { $canvas->enabled_gradientcolor; },
  );
  
  $canvas->createWindow( 100, 100, -window => $bouton_color1 );
  $canvas->createWindow( 400, 100, -window => $bouton_color2 );
  $canvas->createWindow( 100, 150, -window => $start_num );
  $canvas->createWindow( 100, 200, -window => $end_num );
  $canvas->createWindow( 350, 150, -window => $entry_number_color );
  $canvas->createWindow( 350, 200, -window => $type );
  $canvas->createWindow( 100, 350, -window => $disabled_gradientcolor );
  $canvas->createWindow( 400, 350, -window => $enabled_gradientcolor );
  
  MainLoop;

AUTHOR

Djibril Ousmanou, <djibel at cpan.org>

BUGS

Please report any bugs or feature requests to bug-Tk-Canvas-GradientColor at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tk-Canvas-GradientColor. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SEE ALSO

See Tk::Canvas for details of the standard options.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Tk::Canvas::GradientColor

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

Copyright 2014 Djibril Ousmanou, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.