- coloring people's messages in chat by their user name or their user id
- coloring list of latest users actions, on admin dashboard, by user id or username
- clever color picking of the most visually distinct colors (generated by CIEDE2000 algorithm)
- maximum and minimum lightness of returned colors can be set
- fast and lightweight
- lots of customization options
- easy debugging with build in error messages
git clone https://github.com/Hurtak/PHPAutoColor.git
Or you can use Composer
composer require hurtak/phpautocolor:@dev
include "PHPAutoColor/src/PHPAutoColor.php";
$color = new PHPAutoColor();
- customize PHPAutoColor settings or don't do anything and default settings will be used
- integers, numbers with decimal points and strings (case sensitive) are accepted
$color->getColor($userID); // returns "#000"
<?php
// data from DB
$sql = $pdo->prepare("SELECT * FROM actions");
$sql->execute();
$userActions = $sql->fetchAll();
// initial setup
include "PHPAutoColor.php";
$color = new PHPAutoColor();
// PHPAutoColor settings (optional)
$color->setColorType("rgb");
$color->setColorPickingMethod("dynamic");
$color->setLightnessLimit("min", 0.3);
?>
<table>
<tr>
<td>id</td>
<td>user_id</td>
<td>amount</td>
<td>date</td>
</tr>
<?php foreach ($userActions as $action): ?>
<tr style="background-color: <?= $color->getColor($action['user_id']) ?>">
<td><?= $action["id"] ?></td>
<td><?= $action["user_id"] ?></td>
<td><?= $action["amount"] ?></td>
<td><?= $action["date"] ?></td>
</tr>
<?php endforeach ?>
</table>
- returns string of color in hex format (can be changed, see chapter 5.3)
- on error returns empty string (to display errors list, enable debugging, see chapter 5.6)
Parameter | Description |
---|---|
$input |
Input you are basing the coloring around, eg.: user id or user name. Integers, numbers with decimal points and strings (case sensitive) are accepted |
$opacity (optional) |
CSS opacity value, only used if color type is set to "rgba" . Accepted values are numbers in <0 ;1 > range. Default value is set to 1 |
- Specifies what color picking method will be used when
getColor()
is called - This setting is optional, if you won't call this function, default value will be used
$colorPickingMethod | Description |
---|---|
"static" (default) |
Colors are assigned from pregenerated colors list. The same color will always be assigned to certain input across instances of PHPAutoColor |
"dynamic" |
Colors are assigned gradually from pregenerated colors list (first color will always be black, second one white...) |
"dynamic-random" |
Colors are assigned randomly from pregenerated colors list |
"random" |
Colors are assigned randomly |
- Specifies in what format the color will be returned after calling
getColor()
- This setting is optional, if you won't call this function, default value will be used
$colorType | Description |
---|---|
"hex" (default) |
Color in hexadecimal format will be returned. If possible, color will be shortened to 3 digit format. (eg.: #968AE8 , #FFF ) |
"rgb" |
Color in rgb format (eg.: rgb(255,255,255) ) |
"rgba" |
Color in rgba format (eg.: rgba(255,255,255,0.5) ) |
- Limits the maximum or minimum perceived lightness of colors which will be returned after calling
getColor()
. For example, you have white background so you don't wantgetColor()
to return white and other very bright colors, so you usesetLightnessLimit("max", 0.8)
- setLightnessLimit() can be called twice if you want to set
"max"
and"min"
limit at the same time (difference between maximum and minimum lightness must be bigger or equal to0.2
) - This setting is optional, if you won't call this function, default value will be used
$type | $lightness default | Accepted values | Description |
---|---|---|---|
"max" |
1 |
<0.2 ;1 > |
Limits maximum perceived lightness of returned colors |
"min" |
0 |
<0 ;0.8 > |
Limits minimum perceived lightness of returned colors |
- Limits the maximum number of colors with which the PHPAutoColor will work.
- If list of used colors in one instance of PHPAutoColor will hit the
$maximumColors
limit, we start reusing previously assigned colors - This setting is optional, if you won't call this function, number of used colors won't be limited
$maximumColors accepted values | Description |
---|---|
bigger or equal to 2 |
Limits the maximum number of used colors |
- Enables displaying of detected errors
- If errors occur, empty string is returned from
getColor()
method - This setting is optional, if you won't call this function debugging window won't appear
- List of 65 visually most distinct colors generated using CIEDE2000 algorithm
- Colors from this list are used if you use
setColorPickingMethod()
with"static"
(default),"dynamic"
or"dynamic-random"
parameter