|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PackageFactory\ColorHelper\Domain\ValueObject; |
| 6 | + |
| 7 | +abstract class AbstractColor implements ColorInterface |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @return string |
| 11 | + */ |
| 12 | + public function __toString(): string |
| 13 | + { |
| 14 | + return $this->getHexString(); |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * @return string |
| 19 | + */ |
| 20 | + public function getHexString(): string |
| 21 | + { |
| 22 | + $rgba = $this->asRgba(); |
| 23 | + if ($rgba->getAlpha() == 255) { |
| 24 | + return '#' |
| 25 | + .str_pad(dechex($rgba->getRed()), 2, '0') |
| 26 | + .str_pad(dechex($rgba->getGreen()), 2, '0') |
| 27 | + .str_pad(dechex($rgba->getBlue()), 2, '0'); |
| 28 | + } else { |
| 29 | + return '#' |
| 30 | + .str_pad(dechex($rgba->getRed()), 2, '0') |
| 31 | + .str_pad(dechex($rgba->getGreen()), 2, '0') |
| 32 | + .str_pad(dechex($rgba->getBlue()), 2, '0') |
| 33 | + .str_pad(dechex($rgba->getAlpha()), 2, '0'); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return string |
| 39 | + */ |
| 40 | + public function getHslaString(): string |
| 41 | + { |
| 42 | + $hslaColor = $this->asHsla(); |
| 43 | + if ($hslaColor->getAlpha() == 255) { |
| 44 | + return sprintf('hsl(%s, %s%%, %s%%)', $hslaColor->getHue(), $hslaColor->getSaturation(), $hslaColor->getLightness()); |
| 45 | + } else { |
| 46 | + return sprintf('hsla(%s, %s%%, %s%%, %s)', $hslaColor->getHue(), $hslaColor->getSaturation(), $hslaColor->getLightness(), round($hslaColor->getAlpha() / 255, 2)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return string |
| 52 | + */ |
| 53 | + public function getRgbaString(): string |
| 54 | + { |
| 55 | + $rgbColor = $this->asRgba(); |
| 56 | + if ($rgbColor->getAlpha() == 255) { |
| 57 | + return sprintf('rgb(%s, %s, %s)', $rgbColor->getRed(), $rgbColor->getGreen(), $rgbColor->getBlue()); |
| 58 | + } else { |
| 59 | + return sprintf('rgba(%s, %s, %s, %s)', $rgbColor->getRed(), $rgbColor->getGreen(), $rgbColor->getBlue(), $rgbColor->getAlpha()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param ColorInterface $color |
| 65 | + * |
| 66 | + * @return bool |
| 67 | + */ |
| 68 | + public function equals(ColorInterface $color): bool |
| 69 | + { |
| 70 | + return $this->getHexString() == $color->getHexString(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param ColorInterface $color |
| 75 | + * @param int $weight |
| 76 | + * |
| 77 | + * @return RgbaColor |
| 78 | + */ |
| 79 | + public function withMixedColor(ColorInterface $color, int $weight = 50): ColorInterface |
| 80 | + { |
| 81 | + if ($weight < 0 || $weight > 100) { |
| 82 | + throw new \InvalidArgumentException('argument weight has to be an integer between 0 and 100, '.$weight.' was given.'); |
| 83 | + } |
| 84 | + |
| 85 | + $factorA = $weight / 100; |
| 86 | + $factorB = 1 - $factorA; |
| 87 | + |
| 88 | + $rgbaColorA = $this->asRgba(); |
| 89 | + $rgbaColorB = $color->asRgba(); |
| 90 | + |
| 91 | + return new RgbaColor( |
| 92 | + (int) round(($rgbaColorA->getRed() * $factorA) + ($rgbaColorB->getRed() * $factorB)), |
| 93 | + (int) round(($rgbaColorA->getGreen() * $factorA) + ($rgbaColorB->getGreen() * $factorB)), |
| 94 | + (int) round(($rgbaColorA->getBlue() * $factorA) + ($rgbaColorB->getBlue() * $factorB)), |
| 95 | + (int) round(($rgbaColorA->getAlpha() * $factorA) + ($rgbaColorB->getAlpha() * $factorB)) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param int $delta |
| 101 | + * |
| 102 | + * @return ColorInterface |
| 103 | + */ |
| 104 | + public function withAdjustedLightness(int $delta): ColorInterface |
| 105 | + { |
| 106 | + $hslaColor = $this->asHsla(); |
| 107 | + $lightness = $hslaColor->getLightness() + $delta; |
| 108 | + if ($lightness < 0) { |
| 109 | + $lightness = 0; |
| 110 | + } |
| 111 | + if ($lightness > 100) { |
| 112 | + $lightness = 100; |
| 113 | + } |
| 114 | + |
| 115 | + return new HslaColor( |
| 116 | + $hslaColor->getHue(), |
| 117 | + $hslaColor->getSaturation(), |
| 118 | + $lightness, |
| 119 | + $hslaColor->getAlpha() |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param int $delta |
| 125 | + * |
| 126 | + * @return ColorInterface |
| 127 | + */ |
| 128 | + public function withAdjustedSaturation(int $delta): ColorInterface |
| 129 | + { |
| 130 | + $hslaColor = $this->asHsla(); |
| 131 | + $saturation = $hslaColor->getSaturation() + $delta; |
| 132 | + if ($saturation < 0) { |
| 133 | + $saturation = 0; |
| 134 | + } |
| 135 | + if ($saturation > 100) { |
| 136 | + $saturation = 100; |
| 137 | + } |
| 138 | + |
| 139 | + return new HslaColor( |
| 140 | + $hslaColor->getHue(), |
| 141 | + $saturation, |
| 142 | + $hslaColor->getLightness(), |
| 143 | + $hslaColor->getAlpha() |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @param int $delta |
| 149 | + * |
| 150 | + * @return ColorInterface |
| 151 | + */ |
| 152 | + public function withAdjustedHue(int $delta): ColorInterface |
| 153 | + { |
| 154 | + $hslaColor = $this->asHsla(); |
| 155 | + $hue = ($hslaColor->getHue() + $delta) % 360; |
| 156 | + if ($hue < 0) { |
| 157 | + $hue += 360; |
| 158 | + } |
| 159 | + |
| 160 | + return new HslaColor( |
| 161 | + $hue, |
| 162 | + $hslaColor->getSaturation(), |
| 163 | + $hslaColor->getLightness(), |
| 164 | + $hslaColor->getAlpha() |
| 165 | + ); |
| 166 | + } |
| 167 | +} |
0 commit comments