Skip to content

Instantly share code, notes, and snippets.

@jaygilmore
Created October 18, 2024 12:29
Show Gist options
  • Select an option

  • Save jaygilmore/5c679d005ce3a7a6e7392e35d0347285 to your computer and use it in GitHub Desktop.

Select an option

Save jaygilmore/5c679d005ce3a7a6e7392e35d0347285 to your computer and use it in GitHub Desktop.

Revisions

  1. Susan Ottwell revised this gist May 2, 2016. 1 changed file with 32 additions and 31 deletions.
    63 changes: 32 additions & 31 deletions hex2rgba.php
    Original file line number Diff line number Diff line change
    @@ -4,43 +4,44 @@
    # NOTE that the ColorPicker TV must use the default output option
    # based on function from http://mekshq.com/how-to-convert-hexadecimal-color-code-to-rgb-or-rgba-using-php/


    function hex2rgba($color, $opacity = false) {

    $default = 'rgb(0,0,0)';
    if (!function_exists('hex2rgba')) {
    function hex2rgba($color, $opacity = false) {
    $default = 'rgb(0,0,0)';

    //Return default if no color provided
    if(empty($color))
    return $default;
    //Return default if no color provided
    if(empty($color))
    return $default;

    //Sanitize $color if "#" is provided
    if ($color[0] == '#' ) {
    $color = substr( $color, 1 );
    }
    //Sanitize $color if "#" is provided
    if ($color[0] == '#' ) {
    $color = substr( $color, 1 );
    }

    //Check if color has 6 or 3 characters and get values
    if (strlen($color) == 6) {
    $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
    } elseif ( strlen( $color ) == 3 ) {
    $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
    } else {
    return $default;
    }
    //Check if color has 6 or 3 characters and get values
    if (strlen($color) == 6) {
    $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
    } elseif ( strlen( $color ) == 3 ) {
    $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
    } else {
    return $default;
    }

    //Convert hexadec to rgb
    $rgb = array_map('hexdec', $hex);
    //Convert hexadec to rgb
    $rgb = array_map('hexdec', $hex);

    //Check if opacity is set(rgba or rgb)
    if($opacity){
    if(abs($opacity) > 1)
    $opacity = 1.0;
    $output = 'rgba('.implode(",",$rgb).','.$opacity.')';
    } else {
    $output = 'rgb('.implode(",",$rgb).')';
    }
    //Check if opacity is set(rgba or rgb)
    if($opacity){
    if(abs($opacity) > 1)
    $opacity = 1.0;
    $output = 'rgba('.implode(",",$rgb).','.$opacity.')';
    } else {
    $output = 'rgb('.implode(",",$rgb).')';
    }

    //Return rgb(a) color string
    return $output;
    //Return rgb(a) color string
    return $output;
    }

    }

    // get values from output modifier
  2. Susan Ottwell revised this gist May 2, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions hex2rgba.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    <?php
    # hex2rgba
    # converts hex value from ColorPicker TV to rgba value
    # NOTE that the ColorPicker TV must use the default output option
    # based on function from http://mekshq.com/how-to-convert-hexadecimal-color-code-to-rgb-or-rgba-using-php/


  3. Susan Ottwell created this gist May 2, 2016.
    56 changes: 56 additions & 0 deletions hex2rgba.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    <?php
    # hex2rgba
    # converts hex value from ColorPicker TV to rgba value
    # based on function from http://mekshq.com/how-to-convert-hexadecimal-color-code-to-rgb-or-rgba-using-php/


    function hex2rgba($color, $opacity = false) {

    $default = 'rgb(0,0,0)';

    //Return default if no color provided
    if(empty($color))
    return $default;

    //Sanitize $color if "#" is provided
    if ($color[0] == '#' ) {
    $color = substr( $color, 1 );
    }

    //Check if color has 6 or 3 characters and get values
    if (strlen($color) == 6) {
    $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
    } elseif ( strlen( $color ) == 3 ) {
    $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
    } else {
    return $default;
    }

    //Convert hexadec to rgb
    $rgb = array_map('hexdec', $hex);

    //Check if opacity is set(rgba or rgb)
    if($opacity){
    if(abs($opacity) > 1)
    $opacity = 1.0;
    $output = 'rgba('.implode(",",$rgb).','.$opacity.')';
    } else {
    $output = 'rgb('.implode(",",$rgb).')';
    }

    //Return rgb(a) color string
    return $output;
    }

    // get values from output modifier
    $color = $input;
    if ( true === isset($options) ) {
    $opacity = $options;
    } else {
    $opacity = "0.0";
    }

    // run function
    $rgba = hex2rgba($color, $opacity);

    return $rgba;