Showing posts with label Hex color code. Show all posts
Showing posts with label Hex color code. Show all posts

Tuesday, 13 September 2016

PHP Function to Validate Hex Color Code

PHP function to validate the given value for valid hex color code. Validate using this simple php function for valid hex color code.

function validateHexColor($color_code) {
    return (bool)preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i', $color_code);
}

Monday, 29 August 2016

Random Color Generator - Hex Color Code in PHP

Generate random hex color code using this simple random color generator. Creator dynamic background colors using this random color generator in php.

function randomColorGenerator() {
    $colorCode = '#';
    for($i=0; $i<6; $i++) {
        $randValue = rand(0 , 15);
        switch ($randValue) {
            case 10: $randValue = 'A'; break;
            case 11: $randValue = 'B'; break;
            case 12: $randValue = 'C'; break;
            case 13: $randValue = 'D'; break;
            case 14: $randValue = 'E'; break;
            case 15: $randValue = 'F'; break;
        }
        $colorCode .= $randValue;
    }
    return $colorCode;
}