Skip to content

Instantly share code, notes, and snippets.

@RundesBalli
Last active April 2, 2021 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RundesBalli/32f5491df25abb7fe0864e6447a26b75 to your computer and use it in GitHub Desktop.
Save RundesBalli/32f5491df25abb7fe0864e6447a26b75 to your computer and use it in GitHub Desktop.
hex2rgb php-function for calculating the RGB values of a hex string.

hex2rgb

hex2rgb is a simple php function based on the hexdec function and RegEx.

I have been inspired by the two following comments:

Thanks to the authors.

<?php
/**
* hex2rgb function
*
* Calculates a hex code (3 or 6 digit) to RGB.
*
* @author RundesBalli <webspam@rundesballi.com>
* @copyright 2020 RundesBalli
* @version 1.0
* @license MIT-License
* @see https://gist.github.com/RundesBalli/32f5491df25abb7fe0864e6447a26b75
* @see https://www.php.net/manual/en/function.hexdec.php#99478
* @see https://stackoverflow.com/questions/1636350/how-to-identify-a-given-string-is-hex-color-format/1637260#1637260
*
* @param string $hex The hex string to be calculated.
*
* @return array or boolean The RGB array or false.
*/
function hex2rgb($hex) {
if(preg_match("/^(?:(?:[0-9a-f]{2}){3}|(?:[0-9a-f]){3})$/i", preg_replace("/[^0-9a-f]/i", "", $hex), $result) === 1) {
if(strlen($result[0]) == 6) {
return array(
"r" => hexdec(substr($result[0], 0, 2)),
"g" => hexdec(substr($result[0], 2, 2)),
"b" => hexdec(substr($result[0], 4, 2)),
"hex" => $result[0]
);
} elseif(strlen($result[0]) == 3) {
return array(
"r" => hexdec(str_repeat(substr($result[0], 0, 1), 2)),
"g" => hexdec(str_repeat(substr($result[0], 1, 1), 2)),
"b" => hexdec(str_repeat(substr($result[0], 2, 1), 2)),
"hex" => $result[0]
);
} else {
return FALSE;
}
} else {
return FALSE;
}
}
?>

MIT License

Copyright (c) 2021 RundesBalli

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment