Php 优化颜色生成以提高可读性

Php 优化颜色生成以提高可读性,php,colors,Php,Colors,我正在尝试创建一个小脚本,该脚本生成两种颜色,如果一种用作背景,另一种用作字体颜色,将根据以下准则可读: W3C的Web易访问性指南(和不足) 颜色可见性可根据以下内容确定 算法: (这是一个建议的算法,仍有待更改。) 如果亮度不同,两种颜色可提供良好的颜色可见性 而且两种颜色之间的色差大于一组 射程 颜色亮度由以下公式确定:((红色值X 299)+(绿色值X 587)+(蓝色值X 114))/1000注:此 算法取自将RGB值转换为YIQ的公式 价值观此亮度值提供了一个可感知的图像亮度 颜色

我正在尝试创建一个小脚本,该脚本生成两种颜色,如果一种用作背景,另一种用作字体颜色,将根据以下准则可读:

W3C的Web易访问性指南(和不足)

颜色可见性可根据以下内容确定 算法:

(这是一个建议的算法,仍有待更改。)

如果亮度不同,两种颜色可提供良好的颜色可见性 而且两种颜色之间的色差大于一组 射程

颜色亮度由以下公式确定:((红色值X 299)+(绿色值X 587)+(蓝色值X 114))/1000注:此 算法取自将RGB值转换为YIQ的公式 价值观此亮度值提供了一个可感知的图像亮度 颜色

色差由以下公式确定:(最大值(红色 值1,红色值2)-最小值(红色值1,红色值2))+(最大值 (绿色值1,绿色值2)-最小值(绿色值1,绿色值 2) )+(最大值(蓝色值1,蓝色值2)-最小值(蓝色值1, 蓝色值(2))

颜色亮度差的范围为125。颜色的范围 差是500

我的代码是:

do {

    $bg[0] = rand(0, 255);
    $bg[1] = rand(0, 255);
    $bg[2] = rand(0, 255);
    $bg[3] = ($bg[0] + $bg[1] + $bg[2])/1000;

    $txt[0] = rand(0, 255);
    $txt[1] = rand(0, 255);
    $txt[2] = rand(0, 255);
    $txt[3] = ($txt[0] + $txt[1] + $txt[2])/1000;

    //Brightness Difference = Brightness of color 1 - Brightness of color 2
    $brightnessDifference = abs($bg[3] - $txt[3]);

    //Color difference = Maximum (Red1, Red2) - Minimum (Red1, Red2) etc for Blue and Green
    $colorDifference = max($bg[0], $txt[0]) - min($bg[0], $txt[0]) + max($bg[1], $txt[1]) - min($bg[1], $txt[1]) + max($bg[2], $txt[2]) - min($bg[2], $txt[2]);
} while($brightnessDifference < 125 || $colorDifference < 500)
do{
$bg[0]=兰特(0255);
$bg[1]=兰特(0255);
$bg[2]=兰特(0255);
$bg[3]=($bg[0]+$bg[1]+$bg[2])/1000;
$txt[0]=兰特(0255);
$txt[1]=兰特(0255);
$txt[2]=兰特(0255);
$txt[3]=($txt[0]+$txt[1]+$txt[2])/1000;
//亮度差=颜色1的亮度-颜色2的亮度
$brightnessDifference=abs($bg[3]-$txt[3]);
//色差=蓝色和绿色的最大值(红色1,红色2)-最小值(红色1,红色2)等
$colorDifference=max($bg[0],$txt[0])-min($bg[0],$txt[0])+max($bg[1],$txt[1])-min($bg[1],$txt[1])+max($bg[2],$txt[2])-min($bg[2],$txt[2]);
}而($brightnessDifference<125 | |$colorDifference<500)

但是执行时间超过了PHP允许的时间。。。关于如何优化它的建议?:)

您有一个bug,它导致无限循环,使脚本运行时间过长,超过了最大脚本执行时间

代码中的这三行有缺陷:

$bg[3] = ($bg[0] + $bg[1] + $bg[2])/1000;
$txt[3] = ($txt[0] + $txt[1] + $txt[2])/1000;
$brightnessDifference = abs($bg[3] - $txt[3]);
$brightnessDifference
永远不会大于125,因此
while()
将永远运行

以下是从您的问题中引用的解决方案:

颜色亮度由以下公式确定:((红色值X 299)+(绿色值X 587)+(蓝色值X 114))/1000注意:此算法取自将RGB值转换为YIQ值的公式。此亮度值提供颜色的感知亮度

您要求进行优化,尽管删除错误后不需要优化,但可以通过更改
rand()
来优化代码:

do{
$bg[0]=兰特()&255;
$bg[1]=兰特()&255;
$bg[2]=兰特()&255;
$bg[3]=($bg[0]+$bg[1]+$bg[2])/1000;
$txt[0]=rand()&255;
$txt[1]=rand()&255;
$txt[2]=rand()&255;
$txt[3]=($txt[0]+$txt[1]+$txt[2])/1000;
//亮度差=颜色1的亮度-颜色2的亮度
$brightnessDifference=abs($bg[3]-$txt[3]);
//色差=蓝色和绿色的最大值(红色1,红色2)-最小值(红色1,红色2)等
$colorDifference=max($bg[0],$txt[0])-min($bg[0],$txt[0])+max($bg[1],$txt[1])-min($bg[1],$txt[1])+max($bg[2],$txt[2])-min($bg[2],$txt[2]);
}而($brightnessDifference<125 | |$colorDifference<500)
您最多可以节省30%的执行时间

do {

    $bg[0] = rand( ) & 255;
    $bg[1] = rand( ) & 255;
    $bg[2] = rand( ) & 255;
    $bg[3] = ($bg[0] + $bg[1] + $bg[2])/1000;

    $txt[0] = rand( ) & 255;
    $txt[1] = rand( ) & 255;
    $txt[2] = rand( ) & 255;
    $txt[3] = ($txt[0] + $txt[1] + $txt[2])/1000;

    //Brightness Difference = Brightness of color 1 - Brightness of color 2
    $brightnessDifference = abs($bg[3] - $txt[3]);

    //Color difference = Maximum (Red1, Red2) - Minimum (Red1, Red2) etc for Blue and Green
    $colorDifference = max($bg[0], $txt[0]) - min($bg[0], $txt[0]) + max($bg[1], $txt[1]) - min($bg[1], $txt[1]) + max($bg[2], $txt[2]) - min($bg[2], $txt[2]);
} while($brightnessDifference < 125 || $colorDifference < 500)