iwato Posted August 9, 2019 Share Posted August 9, 2019 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 More sharing options...
justsomeguy Posted August 9, 2019 Share Posted August 9, 2019 That's a pretty vague question. And why are you trying to define a function within another function? Link to comment Share on other sites More sharing options...
iwato Posted August 9, 2019 Author Share Posted August 9, 2019 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 More sharing options...
justsomeguy Posted August 9, 2019 Share Posted August 9, 2019 Add a return statement before the recursive call. You want to return the value of the recursive function. 1 Link to comment Share on other sites More sharing options...
iwato Posted August 10, 2019 Author Share Posted August 10, 2019 Very nice. Have a great weekend! Roddy Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now