diff --git a/lib/WebGUI/Image/Color.pm b/lib/WebGUI/Image/Color.pm index 380b9fd9c..c50d846d7 100644 --- a/lib/WebGUI/Image/Color.pm +++ b/lib/WebGUI/Image/Color.pm @@ -4,6 +4,12 @@ use strict; use Color::Calc; #------------------------------------------------------------------- +=head1 canDelete + +Returns true if this color can be deleted. + +=cut + sub canDelete { my $self = shift; @@ -11,6 +17,14 @@ sub canDelete { } #------------------------------------------------------------------- +=head1 copy + +Returns a new WebGUI::Image::Color object being an exact copy of this color, +except for the persistency. This means that the new copy will not be stored in +the database. To accomplish that use the save method on the copy. + +=cut + sub copy { my $self = shift; @@ -18,6 +32,14 @@ sub copy { } #------------------------------------------------------------------- +=head1 darken + +Returns a new WebGUI::Image::Color object with the same properties but the +colors darkened. This object will not be saved to the database automatically. +Use the save method on it if you want to do so. + +=cut + sub darken { my $self = shift; @@ -32,6 +54,13 @@ sub darken { } #------------------------------------------------------------------- +=head1 delete + +Deletes the color from the database. It will only delete if canDelete returns +true. + +=cut + sub delete { my $self = shift; if ($self->canDelete) { @@ -42,6 +71,13 @@ sub delete { } #------------------------------------------------------------------- +=head1 getFillColor + +Returns the the quartet of th fill color. The quartet consists of R, G, B and +Alpha values respectively in HTML format: '#rrggbbaa'. + +=cut + sub getFillColor { my $self = shift; @@ -49,6 +85,12 @@ sub getFillColor { } #------------------------------------------------------------------- +=head1 getFillTriplet + +Returns the RGB triplet of the fill color in HTML format: '#rrggbb'. + +=cut + sub getFillTriplet { my $self = shift; @@ -56,6 +98,12 @@ sub getFillTriplet { } #------------------------------------------------------------------- +=head1 getFillAlpha + +Returns the hex value of the Alpha channel in this color. + +=cut + sub getFillAlpha { my $self = shift; @@ -63,6 +111,12 @@ sub getFillAlpha { } #------------------------------------------------------------------- +=head1 getId + +Returns the GUID of this color. + +=cut + sub getId { my $self = shift; @@ -70,6 +124,12 @@ sub getId { } #------------------------------------------------------------------- +=head1 getName + +Returns the name assigned to this color. + +=cut + sub getName { my $self = shift; @@ -77,6 +137,13 @@ sub getName { } #------------------------------------------------------------------- +=head1 getStrokeColor + +Returns the the quartet of the stroke color. The quartet consists of R, G, B and +Alpha values respectively in HTML format: '#rrggbbaa'. + +=cut + sub getStrokeColor { my $self = shift; @@ -84,6 +151,12 @@ sub getStrokeColor { } #------------------------------------------------------------------- +=head1 getStrokeTriplet + +Returns the RGB triplet of the stroke color in HTML format: '#rrggbb'. + +=cut + sub getStrokeTriplet { my $self = shift; @@ -91,6 +164,12 @@ sub getStrokeTriplet { } #------------------------------------------------------------------- +=head1 getStrokeAlpha + +Returns the hex value of the Alpha channel in the stroke color. + +=cut + sub getStrokeAlpha { my $self = shift; @@ -98,6 +177,46 @@ sub getStrokeAlpha { } #------------------------------------------------------------------- +=head1 new ( session, colorId, [ properties ] ) + +Constructor for this class. + +=head2 session + +A WebGUI::Session object. + +=head2 colorId + +The id of the color you want to instanciate. If you're creating a new color +please use 'new' as id. + +=head2 properties + +A hashref containing configuration options to set this object to. All are also +available through methods. + +=head3 name + +The color name. + +=head3 fillTriplet + +The RGB triplet for the fill color. See setFillTriplet. + +=head3 fillAlpha + +The alpha value for the fill color. See setFillAlpha. + +=head3 strokeTriplet + +The RGB triplet for the stroke color. See setStrokeTriplet. + +=head3 strokeAlpha + +The alpha value for the stroke color. See setStrokeAlpha. + +=cut + sub new { my $class = shift; my $session = shift; @@ -123,6 +242,21 @@ sub new { } #------------------------------------------------------------------- +=head1 newByPalette ( session, paletteId ) + +Returns an arrayref containg instanciated WebGUI::Image::Color objects for each +color in the sepcified palette. + +=head2 session + +A WebGUI::Session object. + +=head2 paletteId + +The id of the palette that is to be loaded. + +=cut + sub newByPalette { my ($sth, $row, @colors); my $class = shift; @@ -142,6 +276,12 @@ sub newByPalette { } #------------------------------------------------------------------- +=head1 session + +Returns the WebGUI::Session object; + +=cut + sub session { my $self = shift; @@ -149,6 +289,16 @@ sub session { } #------------------------------------------------------------------- +=head1 setFillColor ( quartet ) + +Sets the the fill color to the specified quartet. + +=head2 quartet + +The quartet consists of R, G, B and Alpha values respectively in HTML format: '#rrggbbaa'. + +=cut + sub setFillColor { my $self = shift; my $color = shift; @@ -162,6 +312,16 @@ sub setFillColor { } #------------------------------------------------------------------- +=head1 setFillTriplet ( triplet ) + +Sets the RGB triplet of the fill color. + +=head2 triplet + +The RGB triplet in HTML format: '#rrggbb'. + +=cut + sub setFillTriplet { my $self = shift; my $triplet = shift; @@ -175,6 +335,16 @@ sub setFillTriplet { } #------------------------------------------------------------------- +=head1 setFillAlpha ( alpha ) + +Sets the alpha channel for the fill color. + +=head2 alpha + +The alpha value in hexadecimal notation: 'ff'; + +=cut + sub setFillAlpha { my $self = shift; my $alpha = shift; @@ -188,6 +358,16 @@ sub setFillAlpha { } #------------------------------------------------------------------- +=head1 setName ( name ) + +Sets the name of this color. + +=head2 name + +A scalar containing the name of this color. + +=cut + sub setName { my $self = shift; my $name = shift; @@ -197,6 +377,16 @@ sub setName { } #------------------------------------------------------------------- +=head1 setStrokeColor ( quartet ) + +Sets the the stroke color to the specified quartet. + +=head2 quartet + +The quartet consists of R, G, B and Alpha values respectively in HTML format: '#rrggbbaa'. + +=cut + sub setStrokeColor { my $self = shift; my $color = shift; @@ -210,6 +400,16 @@ sub setStrokeColor { } #------------------------------------------------------------------- +=head1 setStrokeTriplet ( triplet ) + +Sets the RGB triplet of the stroke color. + +=head2 triplet + +The RGB triplet in HTML format: '#rrggbb'. + +=cut + sub setStrokeTriplet { my $self = shift; my $triplet = shift; @@ -223,6 +423,16 @@ sub setStrokeTriplet { } #------------------------------------------------------------------- +=head1 setStrokeAlpha ( alpha ) + +Sets the alpha channel for the stroke color. + +=head2 alpha + +The alpha value in hexadecimal notation: 'ff'; + +=cut + sub setStrokeAlpha { my $self = shift; my $alpha = shift; @@ -236,6 +446,14 @@ sub setStrokeAlpha { } #------------------------------------------------------------------- +=head1 update + +Will update the database to the current state of the object. If your object has +not yet been saved to the database, you must first use the save method, which +has the same functionality. + +=cut + sub update { my $self = shift; @@ -250,6 +468,14 @@ sub update { } #------------------------------------------------------------------- +=head1 save + +Will save the state of the object to the database if the color is not yet in the +database. If it already is in the database this method will do exactly the same +as update. + +=cut + sub save { my $self = shift; diff --git a/lib/WebGUI/Image/Palette.pm b/lib/WebGUI/Image/Palette.pm index e11b44fa5..63a33fddb 100644 --- a/lib/WebGUI/Image/Palette.pm +++ b/lib/WebGUI/Image/Palette.pm @@ -4,6 +4,17 @@ use strict; use WebGUI::Image::Color; #------------------------------------------------------------------- +=head1 addColor ( color ) + +Adds a color to this palette. The color will be automatically saved or updated +to the database. + +=head2 color + +A WebGUI::Image::Color object. + +=cut + sub addColor { my $self = shift; my $color = shift; @@ -20,6 +31,12 @@ sub addColor { } #------------------------------------------------------------------- +=head1 canDelete + +Returns true if this palette can be deleted. + +=cut + sub canDelete { my $self = shift; @@ -28,6 +45,12 @@ sub canDelete { } #------------------------------------------------------------------- +=head1 canEdit + +Returns true if this palette can be edited. + +=cut + sub canEdit { my $self = shift; @@ -35,6 +58,15 @@ sub canEdit { } #------------------------------------------------------------------- +=head1 delete + +Deletes the palette from the database. This is only possible if the canDelete +method returns true. + +NOTE: For now the colors in the palette are not deleted automatically. + +=cut + sub delete { my $self = shift; @@ -49,6 +81,14 @@ sub delete { } #------------------------------------------------------------------- +=head1 getColor ( [ index ] ) + +Returns the color at index in the palette. If index is not passed it will return +the color at the index specified by the internal palette index counter, ie. the +current color. + +=cut + sub getColor { my $self = shift; my $index = shift || $self->getPaletteIndex; @@ -57,13 +97,52 @@ sub getColor { } #------------------------------------------------------------------- -sub getColorsInPalette { - my $self = shift; +=head1 getColorIndex ( color ) - return $self->{_palette}; +Returns the index of color. If the color is not in the palette it will return +undef. + +=head2 color + +A WebGUI::Image::Color object. + +=cut + +sub getColorIndex { + my (@palette, $index); + my $self = shift; + my $color = shift; + + my @palette = @{$self->getColorsInPalette}; + + for ($index = 0; $index < scalar(@palette); $index++) { + return $index if ($self->getColor($index)->getId eq $color->getId); + } + + return undef; } #------------------------------------------------------------------- +=head1 getColorsInPalette + +Returns a arrayref containing all color objects in the palette. + +=cut + +sub getColorsInPalette { + my $self = shift; + + # Copy ref so people cannot overwrite + return [ @{$self->{_palette}} ]; +} + +#------------------------------------------------------------------- +=head1 getDefaultPaletteId + +Returns the id of the default palette. + +=cut + sub getDefaultPaletteId { my $self = shift; @@ -71,6 +150,12 @@ sub getDefaultPaletteId { } #------------------------------------------------------------------- +=head1 getId + +Returns the guid of this palette. + +=cut + sub getId { my $self = shift; @@ -78,6 +163,12 @@ sub getId { } #------------------------------------------------------------------- +=head1 getName + +Returns the name of this palette. + +=cut + sub getName { my $self = shift; @@ -85,6 +176,15 @@ sub getName { } #------------------------------------------------------------------- +=head1 getNextColor + +Returns the next color in the palette relative to the internal palette index +counter, and increases this counter to that color. If the counter already is at +the last color in the palette it will cycle around to the first color in the +palette. + +=cut + sub getNextColor { my $self = shift; @@ -96,6 +196,12 @@ sub getNextColor { } #------------------------------------------------------------------- +=head1 getNumberOfColors + +Returns the number of colors in the palette. + +=cut + sub getNumberOfColors { my $self = shift; @@ -103,6 +209,13 @@ sub getNumberOfColors { } #------------------------------------------------------------------- +=head1 getPaletteIndex + +Returns the index the internal palette index counter is set to. Ie. it returns +the current color. + +=cut + sub getPaletteIndex { my $self = shift; @@ -110,6 +223,13 @@ sub getPaletteIndex { } #------------------------------------------------------------------- +=head1 getPaletteList + +Returns a hashref containing a list of all available palettes. The keys are the +palette id's and the value are the names of the palettes. + +=cut + sub getPaletteList { my $self = shift; my $session = shift || $self->session; @@ -118,6 +238,15 @@ sub getPaletteList { } #------------------------------------------------------------------- +=head1 getPreviousColor + +Returns the previous color in the palette relative to the internal palette index +counter, and decreases this counter to that color. If the counter already is at +the first color in the palette it will cycle around to the last color in the +palette. + +=cut + sub getPreviousColor { my $self = shift; @@ -129,6 +258,26 @@ sub getPreviousColor { } #------------------------------------------------------------------- +=head1 new ( session, paletteId, [ name ]) + +Constructor for this class. + +=head2 session + +A WebGUI::Session object. + +=head2 paletteId + +The guid of the palette you want to instanciate. If you want to create a new +palette use 'new' for this value. + +=head2 name + +The name of this palette. If not given it will default to 'untitled'. You can +also adjust this parameter through the setName method. + +=cut + sub new { my ($properties, $colors); my $class = shift; @@ -166,24 +315,47 @@ sub new { } #------------------------------------------------------------------- +=head1 removeColor ( index ) + +Removes color at index. + +NOTE: This method does not delete the color from the database. If you want to do +this you must do it manually. + +=head2 index + +The index of the color you want to remove. If not given nothing will happen. + +=cut + sub removeColor { my $self = shift; - my $color = shift; + my $paletteIndex = shift; - my $newColors = shift; - - foreach (@{$self->{_palette}}) { - push(@$newColors, $_) unless ($_->getId eq $color->getId); - } - $self->{_palette} = $newColors; + return undef unless (defined $paletteIndex); + + my $color = $self->getColor($paletteIndex); + + splice(@{$self->{_palette}}, $paletteIndex, 1); $self->session->db->write('delete from imagePaletteColors where paletteId=? and colorId=?', [ $self->getId, $color->getId, ]); + $self->session->db->write('update imagePaletteColors set paletteOrder=paletteOrder-1 where paletteId=? and paletteOrder > ?', [ + $self->getId, + $paletteIndex, + ]); + } #------------------------------------------------------------------- +=head1 session + +Returns the WebGUI::Session object. + +=cut + sub session { my $self = shift; @@ -191,6 +363,54 @@ sub session { } #------------------------------------------------------------------- +=head1 setColor ( index, color ) + +Sets palette position index to color. This method will automatically save or +update the color. Index must be within the current palette. To add additional +colors use the addColor method. + +=head2 index + +The index within the palette where you want to put the color. + +=head2 color + +The WebGUI::Image::Color object. + +=cut + +sub setColor { + my $self = shift; + my $index = shift; + my $color = shift; + + return undef if ($index >= $self->getNumberOfColors); + return undef if ($index < 0); + return undef unless (defined $index); + return undef unless (defined $color); + + $color->save; + + $self->session->db->write('update imagePaletteColors set colorId=? where paletteId=? and paletteOrder=?', [ + $color->getId, + $self->getId, + $index + 1, + ]); + + $self->{_palette}->[$index] = $color; +} + +#------------------------------------------------------------------- +=head setName ( name ) + +Set the name of this palette. + +=head2 name + +A scalar containing the desired name. + +=cut + sub setName { my $self = shift; my $name = shift; @@ -204,16 +424,55 @@ sub setName { } #------------------------------------------------------------------- +=head1 setPaletteIndex ( index ) + +Set the internal palette index counter. In other words, it sets the current +color to the specified index. If index exceeds the maximum index number it will +be set to the maximum index. + +=head2 index + +The index you want to set the counter to. + +=cut + sub setPaletteIndex { my $self = shift; my $index = shift; + return undef unless (defined $index); + + $index = ($self->getNumberOfColors - 1) if ($index >= $self->getNumberOfColors); + $index = 0 if ($index < 0); + $self->{_paletteIndex} = $index; } #------------------------------------------------------------------- +=head1 swapColors ( firstIndex, secondIndex ) + +Swaps the position of two colors within the palette. + +=head2 firstIndex + +The index of one of the colors to swap. + +=head2 secondIndex + +The index of the other color to swap. + +=cut + sub swapColors { - #### Implementeren! + my $self = shift; + my $indexA = shift; + my $indexB = shift; + + my $colorA = $self->getColor($indexA); + my $colorB = $self->getColor($indexB); + + $self->setColor($indexA, $colorB); + $self->setColor($indexB, $colorA); } 1; diff --git a/lib/WebGUI/Operation.pm b/lib/WebGUI/Operation.pm index af7ffe8e4..a8d72f9d9 100644 --- a/lib/WebGUI/Operation.pm +++ b/lib/WebGUI/Operation.pm @@ -298,6 +298,8 @@ sub getOperations { 'listGraphicsOptions' => 'WebGUI::Operation::Graphics', 'listFonts' => 'WebGUI::Operation::Graphics', 'listPalettes' => 'WebGUI::Operation::Graphics', + 'moveColorDown' => 'WebGUI::Operation::Graphics', + 'moveColorUp' => 'WebGUI::Operation::Graphics', 'removeColorFromPalette' => 'WebGUI::Operation::Graphics', }; } diff --git a/lib/WebGUI/Operation/Graphics.pm b/lib/WebGUI/Operation/Graphics.pm index 0aa2114a6..850529d28 100644 --- a/lib/WebGUI/Operation/Graphics.pm +++ b/lib/WebGUI/Operation/Graphics.pm @@ -51,7 +51,7 @@ sub _getColorForm { -maxlength => 7, -size => 7, ); - $f->text( + $f->hexSlider( -name => 'fillAlpha', -value => $color->getFillAlpha, -label => $i18n->get('fill alpha'), @@ -291,8 +291,10 @@ sub www_editPalette { foreach $color (@{$palette->getColorsInPalette}) { $output .= ''; $output .= ''; - $output .= $session->icon->delete('op=removeColorFromPalette&pid='.$palette->getId.'&cid='.$color->getId); - $output .= $session->icon->edit('op=editColor&pid='.$palette->getId.'&cid='.$color->getId); + $output .= $session->icon->delete('op=removeColorFromPalette;pid='.$palette->getId.';index='.$palette->getColorIndex($color)); + $output .= $session->icon->edit('op=editColor;pid='.$palette->getId.';cid='.$color->getId); + $output .= $session->icon->moveUp('op=moveColorUp;pid='.$palette->getId.';index='.$palette->getColorIndex($color)); + $output .= $session->icon->moveDown('op=moveColorDown;pid='.$palette->getId.';index='.$palette->getColorIndex($color)); $output .= ''; $output .= ''; $output .= ''; @@ -362,6 +364,36 @@ sub www_listPalettes { return _submenu($session, $output); } +#------------------------------------------------------------------- +sub www_moveColorDown { + my ($palette, $index); + my $session = shift; + + $palette = WebGUI::Image::Palette->new($session, $session->form->process('pid')); + $index = $session->form->process('index'); + + if ($index < ($palette->getNumberOfColors - 1) && $index >=0) { + $palette->swapColors($index, $index + 1); + } + + return www_editPalette($session, $session->form->process('pid')); +} + +#------------------------------------------------------------------- +sub www_moveColorUp { + my ($palette, $index); + my $session = shift; + + $palette = WebGUI::Image::Palette->new($session, $session->form->process('pid')); + $index = $session->form->process('index'); + + if ($index <= ($palette->getNumberOfColors - 1) && $index > 0) { + $palette->swapColors($index, $index - 1); + } + + return www_editPalette($session, $session->form->process('pid')); +} + #------------------------------------------------------------------- sub www_listFonts { my ($output); @@ -398,7 +430,7 @@ sub www_removeColorFromPalette { return $session->privilege->adminOnly() unless ($session->user->isInGroup(3)); my $palette = WebGUI::Image::Palette->new($session, $session->form->process('pid')); - $palette->removeColor(WebGUI::Image::Color->new($session, $session->form->process('cid'))); + $palette->removeColor($session->form->process('index')); return www_editPalette($session, $session->form->process('pid')); }