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     * Default Link Color
43     * Saturation and lightness comes from the
44     * Note:
45     *   * blue color of Bootstrap #0d6efd s: 98, l: 52
46     *   * blue color of twitter #1d9bf0 s: 88, l: 53
47     *   * reddit gray with s: 16, l : 31
48     *   * the text is s: 11, l: 15
49     * We choose the gray/tone rendering to be close to black
50     * the color of the text
51     */
52    public static function toTextHoverColor(ColorRgb $colorRgb): ColorRgb
53    {
54        try {
55            return $colorRgb
56                ->toHsl()
57                ->setSaturation(88)
58                ->setLightness(53)
59                ->toRgb()
60                ->toMinimumContrastRatioAgainstWhite();
61        } catch (ExceptionCompile $e) {
62            LogUtility::error("Error while calculating the color text hover color. {$e->getMessage()}", self::CANONICAL, $e);
63            return $colorRgb;
64        }
65    }
66
67    /**
68     *
69     * @throws ExceptionCompile when the color could not be calculated
70     */
71    public static function toBackgroundColor(ColorRgb $primaryColor): ColorRgb
72    {
73        return $primaryColor
74            ->toHsl()
75            ->setLightness(98)
76            ->toRgb()
77            ->toMinimumContrastRatioAgainstWhite(1.1, 1);
78    }
79
80}
81