Jump to content

A Problem of Scope


iwato

Recommended Posts

function select_color_pair($lum_differential, $base = true, $type = true) {
  function random_color($base, %type){
    ... Do something with $base and $true
  }
}

QUESTION:  Is the second appearance of the parameters $base and $type necessary?

Roddy

Link to comment
Share on other sites

DILEMMA:  What I would like following function to do is keep iterating until it has satisfied the condition:

abs($random_lum - $contrast_lum) > $lum_differential

What it does instead is return NULL and stops, when it is unable.

function select_color_pair($base, $lum_differential =0.25){
    $color_pair = array();
    $result = array( 'rgb' => [], 'hex' => '' );
    for( $i=0; $i<3; $i++ ){
        $rand = mt_rand(0, 255);
        $result['rgb'][] = $rand;
        $dechex = dechex($rand);
        if(strlen($dechex) < 2){
            $dechex = '0' . $dechex;
        }
        $result['hex'] .= $dechex;
    }
    if ($base == true) {
        $random_lum = COLOR::fromRGB($result['rgb'])->getLuminance();
        $contrast_color = COLOR::fromRGB($result['rgb'])->invert();
        $contrast_lum = COLOR::fromHex($contrast_color)->getLuminance();
        if (abs($random_lum - $contrast_lum) > $lum_differential) {
            $random_color = COLOR::fromRGB($result['rgb'])->getHex();
            return $color_pair = ['text_color' => $random_color, 'bg_color' => $contrast_color];
        } else {
            select_color_pair($base);
        }
    } else {
        $random_lum = COLOR::fromHex($result['hex'])->getLuminance();	
        $contrast_color = COLOR::fromHex($result['hex'])->invert();
        $contrast_lum = COLOR::fromHex($contrast_color)->getLuminance();
        if (abs($random_lum - $contrast_lum) > $lum_differential) {
            return $color_pair = ['text_color' => strtoupper($result['hex']), 'bg_color' => strtoupper($contrast_color)];
        } else {
            select_color_pair($base);
        }			
    }
}

Any suggestions?

Roddy

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...