Skip to content

Instantly share code, notes, and snippets.

@raicem
Last active June 27, 2017 22:12
Show Gist options
  • Save raicem/325ce0a0e3a41ebf7d8f60f22fa32788 to your computer and use it in GitHub Desktop.
Save raicem/325ce0a0e3a41ebf7d8f60f22fa32788 to your computer and use it in GitHub Desktop.
Hex renk kodlarını rgba formatına dönüştüren fonksiyon ve testler - Function and tests to convert hex color codes to rgba format
{
"require": {
"phpunit/phpunit": "^6.2"
}
}
<?php
/**
* Converts Hex color values into rgba format.
*
* @param string $hex
* @param mixed $alpha
* @return string
*/
function hexToRgb($hex, $alpha = 0)
{
// remove caret symbol from the start
$hex = ltrim($hex, '#');
// if short hex codes used, convert them to full length
if (strlen($hex) === 3) {
$hex = extendShortHex($hex);
}
validateHexColor($hex);
// chunk hex value by two digits to get r, g and b values
$hexArray = str_split($hex, 2);
$hexArray = array_map(function ($item) {
return hexdec($item);
}, $hexArray);
$hexArray['alpha'] = (float)$alpha;
return "rgb($hexArray[0], $hexArray[1], $hexArray[2], $hexArray[alpha])";
}
/**
* Validates hex color values by length and the digits.
*
* @param string $hex
* @throws Error
*/
function validateHexColor($hex)
{
// hex length validation
if (strlen($hex) !== 6) {
throw new Error('Hex code must be 6 digits long.');
}
// hex digit validation
if (ctype_xdigit($hex) === false) {
throw new Error('Please provide a valid hex number.');
}
}
/**
* Short hex codes are converted into six digits.
*
* @param string $hex
* @return string
*/
function extendShortHex($hex)
{
return $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
}
<?php
use PHPUnit\Framework\TestCase;
class HexToRgbTest extends TestCase
{
/**
* Tests if a short hex code can be converted into a normal 6 digit hex code.
*/
public function testShortHexCanBeTurnedIntoLong()
{
$this->assertEquals('FFFFFF', extendShortHex('FFF'));
$this->assertEquals('FF7700', extendShortHex('F70'));
}
/**
* Tests that an error must be thrown if hex code has invalid number of characters.
*/
public function testHexLengthValidation()
{
$this->expectException(Error::class);
hexToRgb('FFFFF', 1);
}
/**
* Tests if hex color code has digits that are not in hexadecimal system.
*/
public function testHexValidation()
{
$this->expectException(Error::class);
hexToRgb('FFFFFG');
}
/**
* Tests a hex color code can be converted to rgba format as string.
*/
public function testHexCanBeConvertedToRgb()
{
$this->assertEquals('rgb(255, 255, 255, 0.3)', hexToRgb('#FFF', '0.3'));
$this->assertEquals('rgb(255, 255, 255, 1)', hexToRgb('#FFFFFF', 1));
$this->assertEquals('rgb(255, 255, 255, 0.5)', hexToRgb('FFF', '.5'));
$this->assertEquals('rgb(255, 255, 255, 1)', hexToRgb('FFFFFF', 1));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="HexToRgb.php">
<testsuites>
<testsuite name="Hex to RGB Tests">
<directory>./HexToRgbTest.php</directory>
</testsuite>
</testsuites>
</phpunit>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment