Skip to content

Commit ba8516e

Browse files
committed
Revert alpha channels to integer values
1 parent 4d305cd commit ba8516e

File tree

73 files changed

+847
-626
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+847
-626
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,6 @@ caller, Operation fails after valid input
126126

127127
- Removed topLeftPoint() and bottomRightPoint() from Rectangle::class
128128
- Removed ColorChannelInterface::__construct() from interface
129+
- Removed ColorInterface::toArray()
129130
- Method ImageManagerInterface::animate() is replaced by universal ImageManagerInterface::createImage()
130131
- Method DriverInterface::createAnimation() was removed

src/Colors/AbstractColor.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,6 @@ public function normalizedChannelValues(): array
6565
);
6666
}
6767

68-
/**
69-
* {@inheritdoc}
70-
*
71-
* @see ColorInterface::toArray()
72-
*/
73-
public function toArray(): array
74-
{
75-
return array_map(
76-
fn(ColorChannelInterface $channel): int|float => $channel->value(),
77-
$this->channels()
78-
);
79-
}
80-
8168
/**
8269
* {@inheritdoc}
8370
*
@@ -110,7 +97,7 @@ public function __debugInfo(): array
11097
{
11198
return array_reduce($this->channels(), function (array $result, ColorChannelInterface $item) {
11299
$key = strtolower((new ReflectionClass($item))->getShortName());
113-
$result[$key] = $item->value();
100+
$result[$key] = $item->toString();
114101
return $result;
115102
}, []);
116103
}

src/Colors/AlphaColorChannel.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,51 @@
44

55
namespace Intervention\Image\Colors;
66

7-
abstract class AlphaColorChannel extends FloatColorChannel
7+
use Intervention\Image\Exceptions\InvalidArgumentException;
8+
9+
abstract class AlphaColorChannel extends AbstractColorChannel
810
{
11+
/**
12+
* Main color channel value
13+
*/
14+
protected int $value;
15+
16+
/**
17+
* @throws InvalidArgumentException
18+
*/
19+
final public function __construct(float $value)
20+
{
21+
$this->value = intval(round($this->validValueOrFail($value) * $this->max()));
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*
27+
* @see ColorChannelInterface::fromNormalized()
28+
*
29+
* @throws InvalidArgumentException
30+
*/
31+
public static function fromNormalized(float $normalized): self
32+
{
33+
if ($normalized < 0 || $normalized > 1) {
34+
throw new InvalidArgumentException(
35+
'Normalized color channel value of ' . static::class . ' must be in range 0 to 1',
36+
);
37+
}
38+
39+
return new static($normalized);
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*
45+
* @see ColorChannelInterface::value()
46+
*/
47+
public function value(): int
48+
{
49+
return $this->value;
50+
}
51+
952
/**
1053
* {@inheritdoc}
1154
*
@@ -23,6 +66,16 @@ public static function min(): float
2366
*/
2467
public static function max(): float
2568
{
26-
return 1;
69+
return 255;
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*
75+
* @see ColorChannelInterface::toString()
76+
*/
77+
public function toString(): string
78+
{
79+
return strval(round($this->value() / $this->max(), 2));
2780
}
2881
}

src/Colors/Cmyk/Color.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ class Color extends AbstractColor
2323
/**
2424
* Create new instance.
2525
*/
26-
public function __construct(int $c, int $m, int $y, int $k)
26+
public function __construct(int|Cyan $c, int|Magenta $m, int|Yellow $y, int|Key $k)
2727
{
28-
/** @throws void */
2928
$this->channels = [
30-
new Cyan($c),
31-
new Magenta($m),
32-
new Yellow($y),
33-
new Key($k),
29+
is_int($c) ? new Cyan($c) : $c,
30+
is_int($m) ? new Magenta($m) : $m,
31+
is_int($y) ? new Yellow($y) : $y,
32+
is_int($k) ? new Key($k) : $k,
3433
];
3534
}
3635

src/Colors/Cmyk/Colorspace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Colorspace implements ColorspaceInterface
3939
public static function colorFromNormalized(array $normalized): CmykColor
4040
{
4141
return new Color(...array_map(
42-
fn(string $classname, float $normalized) => $classname::fromNormalized($normalized)->value(),
42+
fn(string $classname, float $normalized) => $classname::fromNormalized($normalized),
4343
self::$channels,
4444
$normalized,
4545
));

src/Colors/Hsl/Color.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Intervention\Image\Colors\Hsl\Channels\Hue;
1010
use Intervention\Image\Colors\Hsl\Channels\Luminance;
1111
use Intervention\Image\Colors\Hsl\Channels\Saturation;
12-
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
12+
use Intervention\Image\Colors\Rgb\Colorspace as Rgb;
1313
use Intervention\Image\Exceptions\ColorDecoderException;
1414
use Intervention\Image\Exceptions\DriverException;
1515
use Intervention\Image\Exceptions\InvalidArgumentException;
@@ -23,14 +23,13 @@ class Color extends AbstractColor
2323
/**
2424
* Create new color object.
2525
*/
26-
public function __construct(int $h, int $s, int $l, float $a = 1)
26+
public function __construct(int|Hue $h, int|Saturation $s, int|Luminance $l, float|Alpha $a = 1)
2727
{
28-
/** @throws void */
2928
$this->channels = [
30-
new Hue($h),
31-
new Saturation($s),
32-
new Luminance($l),
33-
new Alpha($a),
29+
is_int($h) ? new Hue($h) : $h,
30+
is_int($s) ? new Saturation($s) : $s,
31+
is_int($l) ? new Luminance($l) : $l,
32+
is_float($a) ? new Alpha($a) : $a,
3433
];
3534
}
3635

@@ -137,7 +136,7 @@ public function toHex(string $prefix = ''): string
137136
);
138137
}
139138

140-
return $this->toColorspace(RgbColorspace::class)->toHex($prefix);
139+
return $this->toColorspace(Rgb::class)->toHex($prefix);
141140
}
142141

143142
/**
@@ -147,8 +146,18 @@ public function toHex(string $prefix = ''): string
147146
*/
148147
public function toString(): string
149148
{
149+
if ($this->isTransparent()) {
150+
return sprintf(
151+
'hsl(%d %d %d / %s)',
152+
$this->hue()->value(),
153+
$this->saturation()->value(),
154+
$this->luminance()->value(),
155+
$this->alpha()->toString(),
156+
);
157+
}
158+
150159
return sprintf(
151-
'hsl(%d %d%% %d%%)',
160+
'hsl(%d %d %d)',
152161
$this->hue()->value(),
153162
$this->saturation()->value(),
154163
$this->luminance()->value()

src/Colors/Hsl/Colorspace.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function colorFromNormalized(array $normalized): HslColor
4848
return new Color(...array_map(
4949
function (string $channel, null|float $normalized) {
5050
try {
51-
return $channel::fromNormalized($normalized)->value();
51+
return $channel::fromNormalized($normalized);
5252
} catch (TypeError $e) {
5353
throw new InvalidArgumentException(
5454
'Normalized color value must be in range 0 to 1',
@@ -122,7 +122,7 @@ private function importRgbColor(RgbColor $color): HslColor
122122
intval(round($hue)),
123123
intval(round($saturation * 100)),
124124
intval(round($luminance * 100)),
125-
$color->alpha()->value(),
125+
$color->alpha()->normalizedValue(),
126126
);
127127
}
128128

@@ -152,7 +152,7 @@ private function importHsvColor(HsvColor $color): HslColor
152152
intval(round($h * 360)),
153153
intval(round($saturation * 100)),
154154
intval(round($luminance * 100)),
155-
$color->alpha()->value(),
155+
$color->alpha()->normalizedValue(),
156156
);
157157
}
158158

src/Colors/Hsv/Color.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ class Color extends AbstractColor
2323
/**
2424
* Create new color object.
2525
*/
26-
public function __construct(int $h, int $s, int $v, float $a = 1)
26+
public function __construct(int|Hue $h, int|Saturation $s, int|Value $v, float|Alpha $a = 1)
2727
{
28-
/** @throws void */
2928
$this->channels = [
30-
new Hue($h),
31-
new Saturation($s),
32-
new Value($v),
33-
new Alpha($a),
29+
is_int($h) ? new Hue($h) : $h,
30+
is_int($s) ? new Saturation($s) : $s,
31+
is_int($v) ? new Value($v) : $v,
32+
is_float($a) ? new Alpha($a) : $a,
3433
];
3534
}
3635

@@ -152,7 +151,7 @@ public function toString(): string
152151
$this->hue()->value(),
153152
$this->saturation()->value(),
154153
$this->value()->value(),
155-
round($this->alpha()->value(), 2),
154+
$this->alpha()->toString(),
156155
);
157156
}
158157

src/Colors/Hsv/Colorspace.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function colorFromNormalized(array $normalized): HsvColor
4848
return new Color(...array_map(
4949
function (string $channel, null|float $normalized) {
5050
try {
51-
return $channel::fromNormalized($normalized)->value();
51+
return $channel::fromNormalized($normalized);
5252
} catch (TypeError $e) {
5353
throw new InvalidArgumentException(
5454
'Normalized color value must be in range 0 to 1',
@@ -127,7 +127,7 @@ private function importRgbColor(RgbColor $color): HsvColor
127127
intval(round($h)),
128128
intval(round($s)),
129129
intval(round($v)),
130-
$color->alpha()->value(),
130+
$color->alpha()->normalizedValue(),
131131
);
132132
}
133133

src/Colors/Oklab/Color.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
namespace Intervention\Image\Colors\Oklab;
66

77
use Intervention\Image\Colors\AbstractColor;
8+
use Intervention\Image\Colors\Oklab\Channels\A;
9+
use Intervention\Image\Colors\Oklab\Channels\B;
10+
use Intervention\Image\Colors\Oklab\Channels\Alpha;
11+
use Intervention\Image\Colors\Oklab\Channels\Lightness;
812
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
913
use Intervention\Image\Exceptions\ColorDecoderException;
1014
use Intervention\Image\Exceptions\DriverException;
@@ -19,14 +23,13 @@ class Color extends AbstractColor
1923
/**
2024
* Create new color object.
2125
*/
22-
public function __construct(float $l, float $a, float $b, float $alpha = 1)
26+
public function __construct(float|Lightness $l, float|A $a, float|B $b, float|Alpha $alpha = 1)
2327
{
24-
/** @throws void */
2528
$this->channels = [
26-
new Channels\Lightness($l),
27-
new Channels\A($a),
28-
new Channels\B($b),
29-
new Channels\Alpha($alpha),
29+
is_float($l) ? new Lightness($l) : $l,
30+
is_float($a) ? new A($a) : $a,
31+
is_float($b) ? new B($b) : $b,
32+
is_float($alpha) ? new Alpha($alpha) : $alpha,
3033
];
3134
}
3235

@@ -86,7 +89,7 @@ public function colorspace(): ColorspaceInterface
8689
public function lightness(): ColorChannelInterface
8790
{
8891
/** @throws void */
89-
return $this->channel(Channels\Lightness::class);
92+
return $this->channel(Lightness::class);
9093
}
9194

9295
/**
@@ -95,7 +98,7 @@ public function lightness(): ColorChannelInterface
9598
public function a(): ColorChannelInterface
9699
{
97100
/** @throws void */
98-
return $this->channel(Channels\A::class);
101+
return $this->channel(A::class);
99102
}
100103

101104
/**
@@ -104,7 +107,7 @@ public function a(): ColorChannelInterface
104107
public function b(): ColorChannelInterface
105108
{
106109
/** @throws void */
107-
return $this->channel(Channels\B::class);
110+
return $this->channel(B::class);
108111
}
109112

110113
/**
@@ -113,7 +116,7 @@ public function b(): ColorChannelInterface
113116
public function alpha(): ColorChannelInterface
114117
{
115118
/** @throws void */
116-
return $this->channel(Channels\Alpha::class);
119+
return $this->channel(Alpha::class);
117120
}
118121

119122
/**
@@ -148,7 +151,7 @@ public function toString(): string
148151
$this->lightness()->value(),
149152
$this->a()->value(),
150153
$this->b()->value(),
151-
round($this->alpha()->value(), 2)
154+
$this->alpha()->toString(),
152155
);
153156
}
154157

0 commit comments

Comments
 (0)