www.webdeveloper.com
Results 1 to 2 of 2

Thread: Color Picker with Hex to RGB

  1. #1
    Join Date
    May 2014
    Posts
    7

    Color Picker with Hex to RGB

    I am using the jscolor color picker then needing to convert the hex to RBG to output this via the FPDF library.

    I am trying to use this script to convert the color
    Code:
    function hex2rgb($hex) {
       $hex = str_replace("#", "", $hex);
    
       if(strlen($hex) == 3) {
          $r = hexdec(substr($hex,0,1).substr($hex,0,1));
          $g = hexdec(substr($hex,1,1).substr($hex,1,1));
          $b = hexdec(substr($hex,2,1).substr($hex,2,1));
       } else {
          $r = hexdec(substr($hex,0,2));
          $g = hexdec(substr($hex,2,2));
          $b = hexdec(substr($hex,4,2));
       }
       $rgb = array($r, $g, $b);
       //return implode(",", $rgb); // returns the rgb values separated by commas
       return $rgb; // returns an array with the rgb values
    }

    This is my line for collecting the hex(same page as function)
    $color = $_POST["value"];

    and this is my html
    HTML Code:
    <form action="text-color.php" name="genForm" method="post">
    
     Color of Text <input class="color" value="000000">
    		<br />
    
    <input type="submit" value="Get Color" style="height: 40px; width: 180px; font-size: 17px;" />
    
    </form>

    Since FPDF can only output RGB I need to be able change this
    SetTextColor(int r [, int g, int b]) via the function, but can't get it to work.



    The PHP/PDF pages are set up correctly, as I am already outputting other information. This is the only part I can't get to work.

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    So...how/where are you using the function? Looks like you would supply it the hex-code string you get from the form, and it will return an array of 3 values.

    However, looking at your HTML form, you don't have a name attribute for the field, so maybe that's your problem?
    HTML Code:
    <input name="color" class="color" value="000000">
    Then you should be able to do something like:
    PHP Code:
    $colors hex2rgb($_POST['color']);

    some_magical_pdf_function($colors[0], $colors[1], $colors[2]); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles