/    Sign up×
Community /Pin to ProfileBookmark

Color Picker with Hex to RGB

I am using the [URL=”http://jscolor.com/try.php”]jscolor[/URL] 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
}[/CODE]

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

and this is my html

[code=html]<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>

[/code]

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

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 08.2014 — 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?
[code=html]
<input name="color" class="color" value="000000">
[/code]

Then you should be able to do something like:
[code=php]
$colors = hex2rgb($_POST['color']);

some_magical_pdf_function($colors[0], $colors[1], $colors[2]);
[/code]
×

Success!

Help @lexiferr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...