PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 28, 2020 2:45 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 40 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: ColourPicker class
PostPosted: Tue Jul 11, 2006 1:01 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
Hi all,

I just finished whipping up a quick colour picker and I was wondering if you folks thought it was worth releasing. There are 1001 other colour pickers out there, but this one is way simpler to implement IMHO.

Implementation:
Syntax: [ Download ] [ Hide ]

<html>

 <head>

  <script src = "path/to/ColourPicker.php?mode=js"></script>



  ...





  <form name = "myForm">

   <input type = "text" name = "myCode" />

   <input type = "submit" onClick = "newColourPicker('myForm.myCode');return false;" />

  </form>

 



This URL will change if this gets approved

And now the code:

Syntax: [ Download ] [ Hide ]

<?PHP



if($_GET['mode'] == 'js')

{

  header('Content-type: text/javascript');

  $window_url = $_SERVER['SCRIPT_NAME'];

 



  echo <<<JS

    function newColourPicker(id)

    {

      var myWindow = window.open('$window_url?element=' + id,'colourPicker','height=200, width=160, menubar=no, resizable=yes, scrollbars=no, status=no, titlebar=no, toolbar=no');

    }

JS
;

exit();

}



?>





<html>

 <head>

  <style type = "text/css">

    body{

     margin:0px;

     padding:0px;

   }

  </style>

  <title>

   Colour Picker

  </title>

  <script type = "text/javascript">

  function setWellColour(colour)

  {

    document.getElementById('well').style.backgroundColor = colour;

  }

  function convertToHex(colour)

  {

    var hex_r;

    var hex_g;

    var hex_b;



    pattern = /rgb\((.*)\)/;

    var match = pattern.exec(colour);



    //if the colour was in the format rgb(RRR,GGG,BBB) (we're in Firefox)

    if(match)

    {

      var rgb = match[1].split(", ");

      var r = rgb[0];

      var g = rgb[1];

      var b = rgb[2];

 

      hex_r = (r - 0).toString(16);

      hex_g = (g - 0).toString(16);

      hex_b = (b - 0).toString(16);

 

      final_code = (hex_r.length == 2) ? hex_r : hex_r + hex_r;

      final_code += (hex_g.length == 2) ? hex_g : hex_g + hex_g;

      final_code += (hex_b.length == 2) ? hex_b : hex_b + hex_b;

 

      return(final_code);

    }

   

    //if it didn't match, we must be in IE

    else

    {

      hex_r = colour.substring(1,2);

      hex_g = colour.substring(2,3);

      hex_b = colour.substring(3,4);

      return hex_r + hex_r + hex_g + hex_g + hex_b + hex_b;

    }

  }

  </script>

 </head>

 <body>



<?PHP

$element = $_GET['element'];

$ColourPicker = new ColourPicker($element);

$ColourPicker->go();

?>



<div style = "border:1px solid #333;height:32px;width:152px;padding:3px;">

 <div id = "well" style = "font-family:Verdana,sans-serif;font-size:8pt;height:32px;width:152px;">

 </div>

</div>



<?PHP

class ColourPicker

{

  var $step;

  var $colour_max;

  var $dimension;

  var $font_size;

  var $element_id;



  function ColourPicker($p_element_id)

  {

    $this->step = 1;

    $this->colour_max = 15;

    $this->dimension = '5px';

    $this->font_size = '3pt';

    $this->element_id = $p_element_id;

  }



  function go()

  {

    echo "<div style = 'font-size:$this->font_size;'>";



    //----------------

    //dump green & blue

    //Top left: turqoise

    //Bottom right: black





    $start_g = $this->colour_max;

    $end_g = 0;

    $start_b = $this->colour_max;

    $end_b = 0;



    echo '<div style = "float:left;">';

    $this->dumpGrid($start_g,$end_g,$start_b,$end_b,'g','b');

    echo '</div>';



    //-----------------

    //dump green & red

    //Top left: green

    //Bottom right: red



    $start_g = $this->colour_max;

    $end_g = 0;

    $start_r = 0;

    $end_r = $this->colour_max;



    echo '<div>';

    $this->dumpGrid($start_g,$end_g,$start_r,$end_r,'g','r');

    echo '</div>';



    //-----------------

    //dump blue & red

    //Top left: blue

    //Bottom right: red



    $start_b = $this->colour_max;

    $end_b = 0;

    $start_r = 0;

    $end_r = $this->colour_max;





    echo '<div style = "float:left;">';

    $this->dumpGrid($start_r,$end_r,$start_b,$end_b,'r','b');

    echo '</div>';



    echo '<div>';

    $this->dumpMonochromaticGrid();

    echo '</div>';

  }



  function dumpGrid($start_x,$end_x,$start_y,$end_y,$x_rgb,$y_rgb)

  {

    $working_start_x = ($start_x > $end_x) ? $start_x * -1 : $start_x;

    $working_end_x = ($start_x > $end_x) ? $end_x * -1 : $end_x;

    $invert_x = ($start_x > $end_x) ? true : false;



    $working_start_y = ($start_y > $end_y) ? $start_y * -1 : $start_y;

    $working_end_y = ($start_y > $end_y) ? $end_y * -1 : $end_y;

    $invert_y = ($start_y > $end_y) ? true : false;



    for($i = $working_start_x; $i <= $working_end_x; $i += $this->step)

    {

      $x = ($invert_x) ? dechex($i * -1) : dechex($i);

     

      for($j = $working_start_y; $j <= $working_end_y; $j += $this->step)

      {

        $y = ($invert_y) ? dechex($j * -1) : dechex($j);



       

        $r = ($x_rgb == 'r') ? $x : 0;

        $r = ($y_rgb == 'r') ? $y : $r;



        $g = ($x_rgb == 'g') ? $x : 0;

        $g = ($y_rgb == 'g') ? $y : $g;



        $b = ($x_rgb == 'b') ? $x : 0;

        $b = ($y_rgb == 'b') ? $y : $b;



        $this->outputColourSquare($r,$g,$b);

      }

      echo '<br />';

    }

  }





  function dumpMonochromaticGrid()

  {

    for($row = 0; $row <= $this->colour_max;$row += $this->step)

    {

      for($value = 0; $value <= $this->colour_max;$value += $this->step)

      {

        $hex_value = dechex($value);



        $this->outputColourSquare($hex_value,$hex_value,$hex_value);

      }

      echo '<br />';

    }

  }



  function outputColourSquare($r,$g,$b)

  {

    echo <<<SQUARE

<div style = "cursor:pointer;float:left;background-color:#{$r}{$g}{$b};width:$this->dimension;height:$this->dimension;"

     onMouseOver = "setWellColour(this.style.backgroundColor);"

     onClick = "window.opener.document.$this->element_id.value = convertToHex(this.style.backgroundColor);window.close();">&nbsp;</div>

SQUARE
;

  }

}

?>

 




If anyone can come up with any way to optimize this further, please don't hesitate to let me know!

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 1:11 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 1:20 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 2:30 pm 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 2:35 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 2:55 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 2:59 pm 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 3:11 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 3:19 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 3:22 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 3:29 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 3:33 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
Oh ok. I understand now.

No. ;)

I like it layed out the way it is so I'm not planning on changing it unless I can get some hella speed increases.

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 4:11 pm 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 4:18 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 4:21 pm 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 40 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group