xref: /plugin/combo/ComboStrap/ColorSystem.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3namespace ComboStrap;
4
5/**
6 * Static method of color calculation
7 */
8class ColorSystem
9{
10
11    const CANONICAL = "color-system";
12
13    /**
14     * Return a color suitable for reading (that has a good ratio)
15     * @param ColorRgb $colorRgb
16     * @return ColorRgb
17     */
18    public static function toTextColor(ColorRgb $colorRgb): ColorRgb
19    {
20        try {
21            return $colorRgb
22                ->toHsl()
23                ->setSaturation(30)
24                ->setLightness(40)
25                ->toRgb()
26                ->toMinimumContrastRatioAgainstWhite();
27        } catch (ExceptionCompile $e) {
28            LogUtility::error("Error while calculating the primary text color. {$e->getMessage()}", self::CANONICAL, $e);
29            return $colorRgb;
30        }
31
32    }
33
34    /**
35     * Calculate a color for a text hover that has:
36     * * more lightness than the text
37     * * and a good contrast ratio
38     *
39     * @param ColorRgb $colorRgb
40     * @return ColorRgb
41     */
42    public static function toTextHoverColor(ColorRgb $colorRgb): ColorRgb
43    {
44        try {
45            return $colorRgb
46                ->toHsl()
47                ->setSaturation(88)
48                ->setLightness(53)
49                ->toRgb()
50                ->toMinimumContrastRatioAgainstWhite();
51        } catch (ExceptionCompile $e) {
52            LogUtility::error("Error while calculating the color text hover color. {$e->getMessage()}", self::CANONICAL, $e);
53            return $colorRgb;
54        }
55    }
56
57    /**
58     *
59     * @throws ExceptionCompile when the color could not be calculated
60     */
61    public static function toBackgroundColor(ColorRgb $primaryColor): ColorRgb
62    {
63        return $primaryColor
64            ->toHsl()
65            ->setLightness(98)
66            ->toRgb()
67            ->toMinimumContrastRatioAgainstWhite(1.1, 1);
68    }
69
70}
71