Can you allocate a color in PHP GD
without an image resource
? It should be possible because really an allocated color is a number, right?
$im = imagecreatetruecolor(100, 100);
$col = imagecolorallocate($im, 255, 0, 0);
print $col."<br/>";
$col2 = imagecolorallocate($im, 255, 0, 0);
print $col2."<br/>";
$im2 = imagecreatetruecolor(600, 100);
$col3 = imagecolorallocate($im, 255, 0, 0);
print $col3;
This prints out:
16711680
16711680
16711680
I guess what the real question is how 255, 0, and 0 are made into 16711680.
16711680 (decimal) is 0x00FF0000 (hexadecimal)
00 - Alpha value (0 dec)
FF - Red (255 dec)
00 - Green (0 dec)
00 - Blue (0 dec)
See http://www.php.net/manual/en/function.imagecolorallocatealpha.php to set the alpha byte
Edit:
Also, to answer your first question -- yes, you can create a color without an image resource (and, consequently without a call to imagecolorallocate):
$col1 = 0x00FF0000; // Red
$col2 = 0x0000FF00; // Green
// etc...
It should be possible because really an allocated color is a number, right?
No, it's not. GD may also have to register that color in the palette of the image (think non true color images).
So you need an image resource.
Use this function
function img_color($r, $g, $b, $a=0) {
// prepare red color
if (is_string($r)) {
if (!preg_match('/^[a-f0-9]{1,2}$/i', $r)) return false;
$r = hexdec($r);
}
elseif (is_int($r)) {if ($r<0 or $r>255) return false;}
else return false;
// prepare green color
if (is_string($g)) {
if (!preg_match('/^[a-f0-9]{1,2}$/i', $g)) return false;
$g = hexdec($g);
}
elseif (is_int($g)) {if ($g<0 or $g>255) return false;}
else return false;
// prepare blue color
if (is_string($b)) {
if (!preg_match('/^[a-f0-9]{1,2}$/i', $b)) return false;
$b = hexdec($b);
}
elseif (is_int($b)) {if ($b<0 or $b>255) return false;}
else return false;
// prepare alpha channel
if (is_string($a)) {
if (!preg_match('/^[a-f0-9]{1,2}$/i', $a)) return false;
$a = hexdec($a);
}
elseif (!is_int($a)) return false;
if ($a<0 or $a>127) return false;
$result = unpack('I', chr($b) . chr($g) . chr($r) . chr($a));
return $result[1];
}
The color index, or color id can be determined several ways. This should help on the road to discovery.
Update ***
I'm not sure why, but after seeing this question I wrote the following function to convert <many>
types of color values to <many>
types of color expressions. Hope it works for someone. *Added CMYK just because. ...
Options -->
'int',
'rgba', 'rgb', 'cmyk',
'hex', 'rgbaCSS', 'rgbCSS', 'hexCSS', 'hexCSS4'
If someone wanted to get really serious, they could write optional arguments for converting the format to any; using sprintf.
<?php
function convertColorValue($colorValue, $toType = 'hex', $recurse = 0) {
if ($recurse > 2) { return $colorValue; }
if (!is_array($colorValue)) {
// OUT --> {Mixed} :: Options --> rgba || rgb || cmyk
// If not an array then we have some form of Number - Valid values int || hex<string>
// Please note that hex numbers such as 0x000000 that are not strings '0x000000' are interpreted by php as their number representation.
// Shortcode hex values.
if (is_string($colorValue) && $colorValue{0} === '#') {
$start = 0;
if (strlen($colorValue) === 4) {
$rgb['alpha'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
} else {
$rgb['alpha'] = 0;
}
$rgb['r'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
$rgb['g'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
$rgb['b'] = hexdec(str_repeat(substr($colorValue, $start, 1), 2));
// We need to make sure this follows some rules before we send it back even if it is the type we are being asked for.
return convertColorValue($rgb, $toType);
}
// Make sure we have a clean sensible string for conversion.
if (preg_match("/[^0-9a-f]/i", $colorValue) !== false) {
if (($colorValue{0} === 0 && $colorValue{1} !== 'x'))
$colorValue = '0x' . $colorValue;
}
$colorValue = preg_replace("/[^0-9a-fx]/i", '', $colorValue); // 4294967295 === 0xFFFFFFFF, the maximum color value.
$colorValue = strlen((string) $colorValue) >= 10 ? (intval(substr($colorValue, 0, 10), 0) > 4294967295 ? 0 : substr($colorValue, 0, 10)) : $colorValue;
// CONVERT our number to base 10 from whatever base it is in.
// **** TODO: Hopefully int or hex, not sure what happens in other bases. ¯\_(ツ)_/¯
// Hex strings should always be prepended with 0x, otherwise confusion happens 11110000 !=? 0x11110000
// If not prepended here with 0x, I can't fix/predict which you are trying to represent int or hex. Int is naturally assumed.
$color = [];
$colorValue = intval(intval($colorValue, 0), 10);
$color['r'] = ($colorValue >> 16) & 0xFF;
$color['g'] = ($colorValue >> 8) & 0xFF;
$color['b'] = $colorValue & 0xFF;
$color['alpha'] = ($colorValue >> 24) & 0x7F;
if ($toType === 'cmyk') {
$c = (255 - $color['r']) / 255.0 * 100;
$m = (255 - $color['g']) / 255.0 * 100;
$y = (255 - $color['b']) / 255.0 * 100;
$b = min([$c,$m,$y]);
$c = $c - $b; $m = $m - $b; $y = $y - $b;
return ['c' => $c, 'm' => $m, 'y' => $y, 'k' => $b, 'alpha' => $color['alpha']];
}
if ($toType === 'rgba' || $toType === 'rgb') {
return $color;
}
return convertColorValue($color, $toType, ++$recurse);
}
// OUT --> {Mixed} :: Options --> int || hex || hexCSS || rgbCSS || rgbaCSS || hexCSS4
// If they've thrown us a c, we are going to assume they gave us CMYK.
if (isset($colorValue['c'])) {
$colorValue['c'] = empty($colorValue['c']) ? 0 : $colorValue['c'] / 100;
$colorValue['m'] = empty($colorValue['m']) ? 0 : $colorValue['m'] / 100;
$colorValue['y'] = empty($colorValue['y']) ? 0 : $colorValue['y'] / 100;
$colorValue['k'] = empty($colorValue['k']) ? 0 : $colorValue['k'] / 100;
$colorValue['r'] = round((1 - ($colorValue['c'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
$colorValue['g'] = round((1 - ($colorValue['m'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
$colorValue['b'] = round((1 - ($colorValue['y'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
} else {
$colorValue['r'] = empty($colorValue['r']) ? 0 : $colorValue['r'];
$colorValue['g'] = empty($colorValue['g']) ? 0 : $colorValue['g'];
$colorValue['b'] = empty($colorValue['b']) ? 0 : $colorValue['b'];
}
$colorValue['alpha'] = (empty($colorValue['alpha']) || $colorValue['alpha'] > 0x7f) ? 0 : $colorValue['alpha'];
if ($toType === 'rgbaCSS') {
$color['alpha'] = empty($color['alpha']) ? 1 :
(round(1 * $color['alpha'] / 127, 3, PHP_ROUND_HALF_DOWN));
return "rgba({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']}, {$colorValue['alpha']})";
}
if ($toType === 'rgbCSS') {
return "rgb({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']})";
}
// Just return the rgb value if opaque since '==' color-values.
if (empty($colorValue['alpha'])) {
$hex = sprintf("%02x%02x%02x", $colorValue['r'], $colorValue['g'], $colorValue['b']);
} else {
$hex = sprintf("%02x%02x%02x%02x", $colorValue['alpha'], $colorValue['r'], $colorValue['g'], $colorValue['b']);
}
$isCSS = ($toType === 'hexCSS4' || $toType === 'hexCSS');
if ($toType === 'hex' || $isCSS) {
return $isCSS ? ("#".$hex) : ("0x".$hex);
}
$hex = "0x".$hex;
if ($toType === 'int') { return hexdec($hex); }
// Is now a hex string. Going back UP^
return convertColorValue($hex, $toType, ++$recurse);
}