Juliagraph/Frame/Panel/ColorBrowser.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Juliagraph/Frame/Panel/Board.pm ↑ Summary Juliagraph/Frame/Panel/ColorPicker.pm »
1  
2 # panel with sliders to select a color
3  
4 package App::GUI::Juliagraph::Frame::Panel::ColorBrowser;
5 1 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Wx;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use base qw/Wx::Panel/;
9 use App::GUI::Juliagraph::Widget::SliderCombo;
10 use App::GUI::Juliagraph::Widget::ColorDisplay;
11 use Graphics::Toolkit::Color qw/color/;
12  
13 sub new {
14 my ( $class, $parent, $type, $init_color ) = @_;
15 $init_color = color( $init_color );
16 return unless ref $init_color;
17  
18 my $self = $class->SUPER::new( $parent, -1);
19 $self->{'init_color'} = $init_color->values( as => 'hash' );
20 $self->{'call_back'} = sub {};
21  
22 my @rgb = $init_color->values('RGB');
23 my @hsl = $init_color->values('HSL');
24  
25 $self->{'widget'}{'red'} = App::GUI::Juliagraph::Widget::SliderCombo->new( $self, 350, ' R ', "red part of $type color", 0, 255, $rgb[0]);
26 $self->{'widget'}{'green'} = App::GUI::Juliagraph::Widget::SliderCombo->new( $self, 350, ' G ', "green part of $type color", 0, 255, $rgb[1]);
27 $self->{'widget'}{'blue'} = App::GUI::Juliagraph::Widget::SliderCombo->new( $self, 350, ' B ', "blue part of $type color", 0, 255, $rgb[2]);
28 $self->{'widget'}{'hue'} = App::GUI::Juliagraph::Widget::SliderCombo->new( $self, 350, ' H ', "hue of $type color", 0, 359, $hsl[0]);
29 $self->{'widget'}{'sat'} = App::GUI::Juliagraph::Widget::SliderCombo->new( $self, 350, ' S ', "saturation of $type color", 0, 100, $hsl[1]);
30 $self->{'widget'}{'light'} = App::GUI::Juliagraph::Widget::SliderCombo->new( $self, 350, ' L ', "lightness of $type color", 0, 100, $hsl[2]);
31 $self->{'button'}{'rnd_'.$_} = Wx::Button->new( $self, -1, '?', [-1,-1], [30,25] ) for qw/red green blue hue sat light/;
32 $self->{'button'}{'rnd_red'}->SetToolTip("randomize red value");
33 $self->{'button'}{'rnd_green'}->SetToolTip("randomize green value");
34 $self->{'button'}{'rnd_blue'}->SetToolTip("randomize blue value");
35 $self->{'button'}{'rnd_hue'}->SetToolTip("randomize hue value");
36 $self->{'button'}{'rnd_sat'}->SetToolTip("randomize saturation value");
37 $self->{'button'}{'rnd_light'}->SetToolTip("randomize lightness value");
38  
39 Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'rnd_red'}, sub { $self->{'widget'}{'red'}->SetValue(int rand 256) });
40 Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'rnd_green'},sub { $self->{'widget'}{'green'}->SetValue(int rand 256) });
41 Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'rnd_blue'}, sub { $self->{'widget'}{'blue'}->SetValue(int rand 256) });
42 Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'rnd_hue'}, sub { $self->{'widget'}{'hue'}->SetValue(int rand 360) });
43 Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'rnd_sat'}, sub { $self->{'widget'}{'sat'}->SetValue(int rand 101) });
44 Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'rnd_light'},sub { $self->{'widget'}{'light'}->SetValue(int rand 101) });
45  
46 my $rgb2hsl = sub {
47 my @rgb = ($self->{'widget'}{'red'}->GetValue,
48 $self->{'widget'}{'green'}->GetValue,
49 $self->{'widget'}{'blue'}->GetValue );
50 my @hsl = color( @rgb )->values('HSL');
51 $self->{'widget'}{'hue'}->SetValue( $hsl[0], 1 );
52 $self->{'widget'}{'sat'}->SetValue( $hsl[1], 1 );
53 $self->{'widget'}{'light'}->SetValue( $hsl[2], 1 );
54 $self->{'call_back'}->( { red => $rgb[0], green => $rgb[1], blue => $rgb[2] } );
55 };
56 my $hsl2rgb = sub {
57 my @hsl = ($self->{'widget'}{'hue'}->GetValue,
58 $self->{'widget'}{'sat'}->GetValue,
59 $self->{'widget'}{'light'}->GetValue );
60 my @rgb = color( 'HSL', @hsl )->values('RGB');
61 $self->{'widget'}{'red'}->SetValue( $rgb[0], 1 );
62 $self->{'widget'}{'green'}->SetValue( $rgb[1], 1 );
63 $self->{'widget'}{'blue'}->SetValue( $rgb[2], 1 );
64 $self->{'call_back'}->( { red => $rgb[0], green => $rgb[1], blue => $rgb[2] } );
65 };
66 $self->{'widget'}{'red'}->SetCallBack( $rgb2hsl );
67 $self->{'widget'}{'green'}->SetCallBack( $rgb2hsl );
68 $self->{'widget'}{'blue'}->SetCallBack( $rgb2hsl );
69 $self->{'widget'}{'hue'}->SetCallBack( $hsl2rgb );
70 $self->{'widget'}{'sat'}->SetCallBack( $hsl2rgb );
71 $self->{'widget'}{'light'}->SetCallBack( $hsl2rgb );
72  
73 my $attr = &Wx::wxALIGN_LEFT | &Wx::wxALIGN_CENTER_HORIZONTAL | &Wx::wxGROW;
74 my $sizer = Wx::BoxSizer->new(&Wx::wxVERTICAL);
75 $sizer->AddSpacer(5);
76 for my $color (qw/red green blue hue sat light/){
77 my $sub_sizer = Wx::BoxSizer->new( &Wx::wxHORIZONTAL );
78 $sub_sizer->Add( $self->{'widget'}{$color}, 0, $attr| &Wx::wxLEFT, 15 );
79 $sub_sizer->Add( 0, 1, &Wx::wxEXPAND | &Wx::wxGROW);
80 $sub_sizer->Add( $self->{'button'}{'rnd_'.$color}, 0, $attr| &Wx::wxRIGHT, 30 );
81 $sizer->Add( $sub_sizer, 0, $attr| &Wx::wxBOTTOM, (($color eq 'blue') ? 25 : 10));
82 }
83 $self->SetSizer($sizer);
84 $self;
85 }
86  
87 # better init color set
88 sub init { $_[0]->set_data( $_[0]->{'init_color'} ) }
89  
90 sub get_data { { red => $_[0]->{'widget'}{'red'}->GetValue,
91 green => $_[0]->{'widget'}{'green'}->GetValue,
92 blue => $_[0]->{'widget'}{'blue'}->GetValue, } }
93  
94 sub set_data {
95 my ( $self, $data, $silent ) = @_;
96 return unless ref $data eq 'HASH'
97 and exists $data->{'red'} and exists $data->{'green'} and exists $data->{'blue'};
98  
99 $self->{'widget'}{'red'}->SetValue( $data->{'red'}, 1);
100 $self->{'widget'}{'green'}->SetValue( $data->{'green'}, 1);
101 $self->{'widget'}{'blue'}->SetValue( $data->{'blue'}, 1 );
102 my @hsl = color( $data )->values('HSL');
103 $self->{'widget'}{'hue'}->SetValue( $hsl[0], 1 );
104 $self->{'widget'}{'sat'}->SetValue( $hsl[1], 1 );
105 $self->{'widget'}{'light'}->SetValue( $hsl[2], 1 );
106 }
107  
108 sub SetCallBack {
109 my ( $self, $code) = @_;
110 return unless ref $code eq 'CODE';
111 $self->{'call_back'} = $code;
112 }
113  
114 1;
« Juliagraph/Frame/Panel/Board.pm ↑ Summary Juliagraph/Frame/Panel/ColorPicker.pm »