PHP生成RGB [英] PHP generate RGB

查看:169
本文介绍了PHP生成RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临这种情况,我有一个来自数据库的ID(所以它可以是1,100,1000,...),我需要生成随机的颜色,但是相等的ID应该导致相同的颜色颜色.

I'm facing this situation where I have an ID which comes from a database (so it can be 1, 100, 1000, ...) and I need to generate random colors, however equal ID's should result in the same color.

关于如何实现这一目标的任何建议?

Any suggestion on how I can achieve this?

谢谢!

推荐答案

使用加密哈希并裁剪不需要的字节:

Use a cryptographic hash and clip the bytes you don't need:

function getColor($num) {
    $hash = md5('color' . $num); // modify 'color' to get a different palette
    return array(
        hexdec(substr($hash, 0, 2)), // r
        hexdec(substr($hash, 2, 2)), // g
        hexdec(substr($hash, 4, 2))); //b
}

生成的(用于生成它的代码)看起来像数字0-20:

The resulting (code to generate it) looks like this for the numbers 0-20:

这篇关于PHP生成RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆