Compare commits
8 Commits
v9.18.1
...
2bd68f32ac
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2bd68f32ac | ||
|
|
f5964fed02 | ||
|
|
625272198d | ||
|
|
00821bd5ea | ||
|
|
921b9cb3d9 | ||
|
|
720b78b687 | ||
|
|
565014e1e2 | ||
|
|
d9bcb577d7 |
@@ -9,7 +9,7 @@ use PHPUnit\Framework\TestCase;
|
|||||||
/**
|
/**
|
||||||
* Test class for Convert\Colors
|
* Test class for Convert\Colors
|
||||||
* @coversDefaultClass \CoreLibs\Convert\Colors
|
* @coversDefaultClass \CoreLibs\Convert\Colors
|
||||||
* @testdox \CoreLibs\Convert\Colors method tests
|
* @testdox \CoreLibs\Convert\Colors legacy method tests
|
||||||
*/
|
*/
|
||||||
final class CoreLibsConvertColorsTest extends TestCase
|
final class CoreLibsConvertColorsTest extends TestCase
|
||||||
{
|
{
|
||||||
@@ -21,7 +21,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function rgb2hexColorProvider(): array
|
public function providerRgb2hexColor(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'color' => [
|
'color' => [
|
||||||
@@ -88,7 +88,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function hex2rgbColorProvider(): array
|
public function providerHex2rgbColor(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'color' => [
|
'color' => [
|
||||||
@@ -215,7 +215,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function rgb2hsbColorProvider(): array
|
public function providerRgb2hsbColor(): array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
||||||
@@ -234,7 +234,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function hsb2rgbColorProvider(): array
|
public function providerHsb2rgbColor(): array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
||||||
@@ -253,7 +253,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function rgb2hslColorProvider(): array
|
public function providerRgb2hslColor(): array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
||||||
@@ -272,7 +272,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function hsl2rgbColorProvider(): array
|
public function providerHsl2rgbColor(): array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
||||||
@@ -291,7 +291,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
* TODO: add cross convert check
|
* TODO: add cross convert check
|
||||||
*
|
*
|
||||||
* @covers ::rgb2hex
|
* @covers ::rgb2hex
|
||||||
* @dataProvider rgb2hexColorProvider
|
* @dataProvider providerRgb2hexColor
|
||||||
* @testdox rgb2hex $input_r,$input_g,$input_b will be $expected [$_dataName]
|
* @testdox rgb2hex $input_r,$input_g,$input_b will be $expected [$_dataName]
|
||||||
*
|
*
|
||||||
* @param int $input_r
|
* @param int $input_r
|
||||||
@@ -342,7 +342,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::hex2rgb
|
* @covers ::hex2rgb
|
||||||
* @dataProvider hex2rgbColorProvider
|
* @dataProvider providerHex2rgbColor
|
||||||
* @testdox hex2rgb $input will be $expected, $expected_str str[,], $expected_str_sep str[$separator] [$_dataName]
|
* @testdox hex2rgb $input will be $expected, $expected_str str[,], $expected_str_sep str[$separator] [$_dataName]
|
||||||
*
|
*
|
||||||
* @param string $input
|
* @param string $input
|
||||||
@@ -385,7 +385,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::rgb2hsb
|
* @covers ::rgb2hsb
|
||||||
* @dataProvider rgb2hsbColorProvider
|
* @dataProvider providerRgb2hsbColor
|
||||||
* @testdox rgb2hsb $input_r,$input_g,$input_b will be $expected [$_dataName]
|
* @testdox rgb2hsb $input_r,$input_g,$input_b will be $expected [$_dataName]
|
||||||
*
|
*
|
||||||
* @param integer $input_r
|
* @param integer $input_r
|
||||||
@@ -409,7 +409,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::hsb2rgb
|
* @covers ::hsb2rgb
|
||||||
* @dataProvider hsb2rgbColorProvider
|
* @dataProvider providerHsb2rgbColor
|
||||||
* @testdox hsb2rgb $input_h,$input_s,$input_b will be $expected [$_dataName]
|
* @testdox hsb2rgb $input_h,$input_s,$input_b will be $expected [$_dataName]
|
||||||
*
|
*
|
||||||
* @param float $input_h
|
* @param float $input_h
|
||||||
@@ -434,7 +434,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::rgb2hsl
|
* @covers ::rgb2hsl
|
||||||
* @dataProvider rgb2hslColorProvider
|
* @dataProvider providerRgb2hslColor
|
||||||
* @testdox rgb2hsl $input_r,$input_g,$input_b will be $expected [$_dataName]
|
* @testdox rgb2hsl $input_r,$input_g,$input_b will be $expected [$_dataName]
|
||||||
*
|
*
|
||||||
* @param integer $input_r
|
* @param integer $input_r
|
||||||
@@ -458,7 +458,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
|||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::hsl2rgb
|
* @covers ::hsl2rgb
|
||||||
* @dataProvider hsl2rgbColorProvider
|
* @dataProvider providerHsl2rgbColor
|
||||||
* @testdox hsl2rgb $input_h,$input_s,$input_l will be $expected [$_dataName]
|
* @testdox hsl2rgb $input_h,$input_s,$input_l will be $expected [$_dataName]
|
||||||
*
|
*
|
||||||
* @param integer|float $input_h
|
* @param integer|float $input_h
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return array<mixed>
|
* @return array<mixed>
|
||||||
*/
|
*/
|
||||||
public function fceilProvider(): array
|
public function providerFceil(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'5.5 must be 6' => [5.5, 6],
|
'5.5 must be 6' => [5.5, 6],
|
||||||
@@ -31,7 +31,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
|||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::fceil
|
* @covers ::fceil
|
||||||
* @dataProvider fceilProvider
|
* @dataProvider providerFceil
|
||||||
* @testdox fceil: Input $input must be $expected
|
* @testdox fceil: Input $input must be $expected
|
||||||
*
|
*
|
||||||
* @param float $input
|
* @param float $input
|
||||||
@@ -51,7 +51,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return array<mixed>
|
* @return array<mixed>
|
||||||
*/
|
*/
|
||||||
public function floorProvider(): array
|
public function providerFloor(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'5123456 with -3 must be 5123000' => [5123456, -3, 5123000],
|
'5123456 with -3 must be 5123000' => [5123456, -3, 5123000],
|
||||||
@@ -63,7 +63,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
|||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::floorp
|
* @covers ::floorp
|
||||||
* @dataProvider floorProvider
|
* @dataProvider providerFloor
|
||||||
* @testdox floor: Input $input with cutoff $cutoff must be $expected
|
* @testdox floor: Input $input with cutoff $cutoff must be $expected
|
||||||
*
|
*
|
||||||
* @param int $input
|
* @param int $input
|
||||||
@@ -84,7 +84,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return array<mixed>
|
* @return array<mixed>
|
||||||
*/
|
*/
|
||||||
public function initNumericProvider(): array
|
public function providerInitNumeric(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'5 must be 5' => [5, 5, 'int'],
|
'5 must be 5' => [5, 5, 'int'],
|
||||||
@@ -98,7 +98,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
|||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::initNumeric
|
* @covers ::initNumeric
|
||||||
* @dataProvider initNumericProvider
|
* @dataProvider providerInitNumeric
|
||||||
* @testdox initNumeric: Input $info $input must match $expected [$_dataName]
|
* @testdox initNumeric: Input $info $input must match $expected [$_dataName]
|
||||||
*
|
*
|
||||||
* @param int|float|string $input
|
* @param int|float|string $input
|
||||||
@@ -113,6 +113,157 @@ final class CoreLibsConvertMathTest extends TestCase
|
|||||||
\CoreLibs\Convert\Math::initNumeric($input)
|
\CoreLibs\Convert\Math::initNumeric($input)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function providerCbrt(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'cube root of 2' => [2, 1.25992, 5],
|
||||||
|
'cube root of 3' => [3, 1.44225, 5],
|
||||||
|
'cube root of -1' => [-1, 'NAN', 0],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @covers ::cbrt
|
||||||
|
* @dataProvider providerCbrt
|
||||||
|
* @testdox initNumeric: Input $input must match $expected [$_dataName]
|
||||||
|
*
|
||||||
|
* @param float|int $number
|
||||||
|
* @param float $expected
|
||||||
|
* @param int $round_to
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCbrt(float|int $number, float|string $expected, int $round_to): void
|
||||||
|
{
|
||||||
|
print "OUT: " . \CoreLibs\Convert\Math::cbrt($number) . "\n";
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
round(\CoreLibs\Convert\Math::cbrt($number), $round_to)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function providerMultiplyMatrices(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'single' => [
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
[14]
|
||||||
|
],
|
||||||
|
'double first' => [
|
||||||
|
[
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3]
|
||||||
|
],
|
||||||
|
[1, 2, 3],
|
||||||
|
[
|
||||||
|
14,
|
||||||
|
14
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'double both' => [
|
||||||
|
[
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[3, 6, 9],
|
||||||
|
[3, 6, 9]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'tripple first, single second' => [
|
||||||
|
[
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
],
|
||||||
|
[1, 2, 3],
|
||||||
|
[
|
||||||
|
14,
|
||||||
|
14,
|
||||||
|
14
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'tripple first, double second' => [
|
||||||
|
[
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[3, 6, 9],
|
||||||
|
[3, 6, 9],
|
||||||
|
[3, 6, 9],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'single first, tripple second' => [
|
||||||
|
[1, 2, 3],
|
||||||
|
[
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[6, 12, 18],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'double first, tripple second' => [
|
||||||
|
[
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
[1, 2, 3],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[6, 12, 18],
|
||||||
|
[6, 12, 18],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @covers ::multiplyMatrices
|
||||||
|
* @dataProvider providerMultiplyMatrices
|
||||||
|
* @testdox initNumeric: Input $input_a x $input_b must match $expected [$_dataName]
|
||||||
|
*
|
||||||
|
* @param array $input_a
|
||||||
|
* @param array $input_b
|
||||||
|
* @param array $expected
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testMultiplyMatrices(array $input_a, array $input_b, array $expected): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
\CoreLibs\Convert\Math::multiplyMatrices($input_a, $input_b)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ parameters:
|
|||||||
# lineAfter: 3
|
# lineAfter: 3
|
||||||
level: 8 # max is now 9
|
level: 8 # max is now 9
|
||||||
# strictRules:
|
# strictRules:
|
||||||
# allRules: true
|
# allRules: false
|
||||||
checkMissingCallableSignature: true
|
checkMissingCallableSignature: true
|
||||||
treatPhpDocTypesAsCertain: false
|
treatPhpDocTypesAsCertain: false
|
||||||
paths:
|
paths:
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ $LOG_FILE_ID = 'classTest-convert-colors';
|
|||||||
ob_end_flush();
|
ob_end_flush();
|
||||||
|
|
||||||
use CoreLibs\Convert\Colors;
|
use CoreLibs\Convert\Colors;
|
||||||
|
use CoreLibs\Convert\Color\Color;
|
||||||
|
use CoreLibs\Convert\Color\Coordinates;
|
||||||
use CoreLibs\Debug\Support as DgS;
|
use CoreLibs\Debug\Support as DgS;
|
||||||
use CoreLibs\Convert\SetVarType;
|
use CoreLibs\Convert\SetVarType;
|
||||||
|
|
||||||
@@ -29,6 +31,36 @@ $log = new CoreLibs\Logging\Logging([
|
|||||||
]);
|
]);
|
||||||
$color_class = 'CoreLibs\Convert\Colors';
|
$color_class = 'CoreLibs\Convert\Colors';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* print out a color block with info
|
||||||
|
*
|
||||||
|
* @param string $color
|
||||||
|
* @param string $text
|
||||||
|
* @param string $text_add
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function display(string $color, string $text, string $text_add): string
|
||||||
|
{
|
||||||
|
$css = 'margin:5px;padding:50px;'
|
||||||
|
. 'width:10%;'
|
||||||
|
. 'text-align:center;'
|
||||||
|
. 'color:white;text-shadow: 0 0 5px black;font-weight:bold;';
|
||||||
|
$template = <<<HTML
|
||||||
|
<div style="background-color:{COLOR};{CSS}">
|
||||||
|
{TEXT}
|
||||||
|
</div>
|
||||||
|
HTML;
|
||||||
|
return str_replace(
|
||||||
|
["{COLOR}", "{TEXT}", "{CSS}"],
|
||||||
|
[
|
||||||
|
$color,
|
||||||
|
$text . ($text_add ? '<br>' . $text_add : ''),
|
||||||
|
$css
|
||||||
|
],
|
||||||
|
$template
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$PAGE_NAME = 'TEST CLASS: CONVERT COLORS';
|
$PAGE_NAME = 'TEST CLASS: CONVERT COLORS';
|
||||||
print "<!DOCTYPE html>";
|
print "<!DOCTYPE html>";
|
||||||
print "<html><head><title>" . $PAGE_NAME . "</title></head>";
|
print "<html><head><title>" . $PAGE_NAME . "</title></head>";
|
||||||
@@ -52,16 +84,16 @@ try {
|
|||||||
print "**Exception: " . $e->getMessage() . "<br><pre>" . print_r($e, true) . "</pre><br>";
|
print "**Exception: " . $e->getMessage() . "<br><pre>" . print_r($e, true) . "</pre><br>";
|
||||||
}
|
}
|
||||||
// B(valid)
|
// B(valid)
|
||||||
$rgb = [10, 20, 30];
|
$rgb = [50, 20, 30];
|
||||||
$hex = '#0a141e';
|
$hex = '#0a141e';
|
||||||
$hsb = [210, 67, 12];
|
$hsb = [210, 67, 12];
|
||||||
$hsb_f = [210.5, 67.5, 12.5];
|
$hsb_f = [210.5, 67.5, 12.5];
|
||||||
$hsl = [210, 50, 7.8];
|
$hsb = [210, 50, 7.8];
|
||||||
print "S::COLOR rgb->hex: $rgb[0], $rgb[1], $rgb[2]: " . Colors::rgb2hex($rgb[0], $rgb[1], $rgb[2]) . "<br>";
|
print "S::COLOR rgb->hex: $rgb[0], $rgb[1], $rgb[2]: " . Colors::rgb2hex($rgb[0], $rgb[1], $rgb[2]) . "<br>";
|
||||||
print "S::COLOR hex->rgb: $hex: " . DgS::printAr(SetVarType::setArray(
|
print "S::COLOR hex->rgb: $hex: " . DgS::printAr(SetVarType::setArray(
|
||||||
Colors::hex2rgb($hex)
|
Colors::hex2rgb($hex)
|
||||||
)) . "<br>";
|
)) . "<br>";
|
||||||
print "C::S/COLOR rgb->hext: $hex: " . DgS::printAr(SetVarType::setArray(
|
print "C::S/COLOR rgb->hex: $hex: " . DgS::printAr(SetVarType::setArray(
|
||||||
CoreLibs\Convert\Colors::hex2rgb($hex)
|
CoreLibs\Convert\Colors::hex2rgb($hex)
|
||||||
)) . "<br>";
|
)) . "<br>";
|
||||||
// C(to hsb/hsl)
|
// C(to hsb/hsl)
|
||||||
@@ -82,9 +114,9 @@ print "S::COLOR hsb_f->rgb: $hsb_f[0], $hsb_f[1], $hsb_f[2]: "
|
|||||||
. DgS::printAr(SetVarType::setArray(
|
. DgS::printAr(SetVarType::setArray(
|
||||||
Colors::hsb2rgb($hsb_f[0], $hsb_f[1], $hsb_f[2])
|
Colors::hsb2rgb($hsb_f[0], $hsb_f[1], $hsb_f[2])
|
||||||
)) . "<br>";
|
)) . "<br>";
|
||||||
print "S::COLOR hsl->rgb: $hsl[0], $hsl[1], $hsl[2]: "
|
print "S::COLOR hsl->rgb: $hsb[0], $hsb[1], $hsb[2]: "
|
||||||
. DgS::printAr(SetVarType::setArray(
|
. DgS::printAr(SetVarType::setArray(
|
||||||
Colors::hsl2rgb($hsl[0], $hsl[1], $hsl[2])
|
Colors::hsl2rgb($hsb[0], $hsb[1], $hsb[2])
|
||||||
)) . "<br>";
|
)) . "<br>";
|
||||||
|
|
||||||
$hsb = [0, 0, 5];
|
$hsb = [0, 0, 5];
|
||||||
@@ -102,8 +134,50 @@ print "RANDOM IN: H: " . $h . ", S: " . $s . ", B/L: " . $b . "/" . $l . "<br>";
|
|||||||
print "RANDOM hsb->rgb: <pre>" . DgS::printAr(SetVarType::setArray(Colors::hsb2rgb($h, $s, $b))) . "</pre><br>";
|
print "RANDOM hsb->rgb: <pre>" . DgS::printAr(SetVarType::setArray(Colors::hsb2rgb($h, $s, $b))) . "</pre><br>";
|
||||||
print "RANDOM hsl->rgb: <pre>" . DgS::printAr(SetVarType::setArray(Colors::hsl2rgb($h, $s, $l))) . "</pre><br>";
|
print "RANDOM hsl->rgb: <pre>" . DgS::printAr(SetVarType::setArray(Colors::hsl2rgb($h, $s, $l))) . "</pre><br>";
|
||||||
|
|
||||||
|
$rgb = [0, 0, 0];
|
||||||
|
print "rgb 0,0,0: " . Dgs::printAr($rgb) . " => " . Dgs::printAr(Colors::rgb2hsb($rgb[0], $rgb[1], $rgb[2])) . "<br>";
|
||||||
|
|
||||||
// TODO: run compare check input must match output
|
// TODO: run compare check input must match output
|
||||||
|
|
||||||
|
$hwb = Color::hsbToHwb(Coordinates\HSB::__constructFromArray([
|
||||||
|
160,
|
||||||
|
0,
|
||||||
|
50,
|
||||||
|
]));
|
||||||
|
print "HWB: " . DgS::printAr($hwb) . "<br>";
|
||||||
|
$hsb = Color::hwbToHsb($hwb);
|
||||||
|
print "HSB: " . DgS::printAr($hsb) . "<br>";
|
||||||
|
|
||||||
|
$oklch = Color::rgbToOkLch(Coordinates\RGB::__constructFromArray([
|
||||||
|
250,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
]));
|
||||||
|
print "OkLch: " . DgS::printAr($oklch) . "<br>";
|
||||||
|
$rgb = Color::okLchToRgb($oklch);
|
||||||
|
print "OkLch -> RGB: " . DgS::printAr($rgb) . "<br>";
|
||||||
|
|
||||||
|
$oklab = Color::rgbToOkLab(Coordinates\RGB::__constructFromArray([
|
||||||
|
250,
|
||||||
|
0,
|
||||||
|
0
|
||||||
|
]));
|
||||||
|
print "OkLab: " . DgS::printAr($oklab) . "<br>";
|
||||||
|
print display($oklab->toCssString(), $oklab->toCssString(), 'Oklab');
|
||||||
|
$rgb = Color::okLabToRgb($oklab);
|
||||||
|
print "OkLab -> RGB: " . DgS::printAr($rgb) . "<br>";
|
||||||
|
print display($rgb->toCssString(), $rgb->toCssString(), 'OkLab to RGB');
|
||||||
|
|
||||||
|
$rgb = Coordinates\RGB::__constructFromArray([250, 100, 10])->toLinear();
|
||||||
|
print "RGBlinear: " . DgS::printAr($rgb) . "<br>";
|
||||||
|
$rgb = Coordinates\RGB::__constructFromArray([0, 0, 0])->toLinear();
|
||||||
|
print "RGBlinear: " . DgS::printAr($rgb) . "<br>";
|
||||||
|
|
||||||
|
$cie_lab = Color::okLabToLab($oklab);
|
||||||
|
print "CieLab: " . DgS::printAr($cie_lab) . "<br>";
|
||||||
|
print display($cie_lab->toCssString(), $cie_lab->toCssString(), 'OkLab to Cie Lab');
|
||||||
|
|
||||||
|
|
||||||
print "</body></html>";
|
print "</body></html>";
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ print "<div>READ _ENV ARRAY:</div>";
|
|||||||
print Support::dumpVar(array_map('htmlentities', $_ENV));
|
print Support::dumpVar(array_map('htmlentities', $_ENV));
|
||||||
// set + check edit access id
|
// set + check edit access id
|
||||||
$edit_access_id = 3;
|
$edit_access_id = 3;
|
||||||
if (is_object($login) && isset($login->loginGetAcl()['unit'])) {
|
if (isset($login->loginGetAcl()['unit'])) {
|
||||||
print "ACL UNIT: " . print_r(array_keys($login->loginGetAcl()['unit']), true) . "<br>";
|
print "ACL UNIT: " . print_r(array_keys($login->loginGetAcl()['unit']), true) . "<br>";
|
||||||
print "ACCESS CHECK: " . (string)$login->loginCheckEditAccess($edit_access_id) . "<br>";
|
print "ACCESS CHECK: " . (string)$login->loginCheckEditAccess($edit_access_id) . "<br>";
|
||||||
if ($login->loginCheckEditAccess($edit_access_id)) {
|
if ($login->loginCheckEditAccess($edit_access_id)) {
|
||||||
@@ -177,25 +177,23 @@ $log->debug('SOME MARK', 'Some error output');
|
|||||||
|
|
||||||
// INTERNAL SET
|
// INTERNAL SET
|
||||||
print "EDIT ACCESS ID: " . $backend->edit_access_id . "<br>";
|
print "EDIT ACCESS ID: " . $backend->edit_access_id . "<br>";
|
||||||
if (is_object($login)) {
|
// print "ACL: <br>".$backend->print_ar($login->loginGetAcl())."<br>";
|
||||||
// print "ACL: <br>".$backend->print_ar($login->loginGetAcl())."<br>";
|
// $log->debug('ACL', "ACL: " . \CoreLibs\Debug\Support::dumpVar($login->loginGetAcl()));
|
||||||
// $log->debug('ACL', "ACL: " . \CoreLibs\Debug\Support::dumpVar($login->loginGetAcl()));
|
// print "DEFAULT ACL: <br>".$backend->print_ar($login->default_acl_list)."<br>";
|
||||||
// print "DEFAULT ACL: <br>".$backend->print_ar($login->default_acl_list)."<br>";
|
// print "DEFAULT ACL: <br>".$backend->print_ar($login->default_acl_list)."<br>";
|
||||||
// print "DEFAULT ACL: <br>".$backend->print_ar($login->default_acl_list)."<br>";
|
// $result = array_flip(
|
||||||
// $result = array_flip(
|
// array_filter(
|
||||||
// array_filter(
|
// array_flip($login->default_acl_list),
|
||||||
// array_flip($login->default_acl_list),
|
// function ($key) {
|
||||||
// function ($key) {
|
// if (is_numeric($key)) {
|
||||||
// if (is_numeric($key)) {
|
// return $key;
|
||||||
// return $key;
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// )
|
||||||
// )
|
// );
|
||||||
// );
|
// print "DEFAULT ACL: <br>".$backend->print_ar($result)."<br>";
|
||||||
// print "DEFAULT ACL: <br>".$backend->print_ar($result)."<br>";
|
// DEPRICATED CALL
|
||||||
// DEPRICATED CALL
|
// $backend->adbSetACL($login->loginGetAcl());
|
||||||
// $backend->adbSetACL($login->loginGetAcl());
|
|
||||||
}
|
|
||||||
|
|
||||||
print "THIS HOST: " . HOST_NAME . ", with PROTOCOL: " . HOST_PROTOCOL . " is running SSL: " . HOST_SSL . "<br>";
|
print "THIS HOST: " . HOST_NAME . ", with PROTOCOL: " . HOST_PROTOCOL . " is running SSL: " . HOST_SSL . "<br>";
|
||||||
print "DIR: " . DIR . "<br>";
|
print "DIR: " . DIR . "<br>";
|
||||||
|
|||||||
344
www/lib/CoreLibs/Convert/Color/CieXyz.php
Normal file
344
www/lib/CoreLibs/Convert/Color/CieXyz.php
Normal file
@@ -0,0 +1,344 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUTHOR: Clemens Schwaighofer
|
||||||
|
* CREATED: 2024/11/12
|
||||||
|
* DESCRIPTION:
|
||||||
|
* CIE XYZ color space conversion
|
||||||
|
* This for various interims work
|
||||||
|
* none public
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert\Color;
|
||||||
|
|
||||||
|
use CoreLibs\Convert\Math;
|
||||||
|
use CoreLibs\Convert\Color\Coordinates\RGB;
|
||||||
|
use CoreLibs\Convert\Color\Coordinates\Lab;
|
||||||
|
use CoreLibs\Convert\Color\Coordinates\XYZ;
|
||||||
|
|
||||||
|
class CieXyz
|
||||||
|
{
|
||||||
|
// MARK: public wrapper functions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert from RGB to OkLab
|
||||||
|
* via xyz D65
|
||||||
|
*
|
||||||
|
* @param RGB $rgb
|
||||||
|
* @return Lab
|
||||||
|
*/
|
||||||
|
public static function rgbViaXyzD65ToOkLab(RGB $rgb): Lab
|
||||||
|
{
|
||||||
|
return self::xyzD65ToOkLab(
|
||||||
|
self::linRgbToXyzD65($rgb)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convet from OkLab to RGB
|
||||||
|
* via xyz D65
|
||||||
|
*
|
||||||
|
* @param Lab $lab
|
||||||
|
* @return RGB
|
||||||
|
*/
|
||||||
|
public static function okLabViaXyzD65ToRgb(Lab $lab): RGB
|
||||||
|
{
|
||||||
|
return self::xyzD65ToLinRgb(
|
||||||
|
self::okLabToXyzD65($lab)
|
||||||
|
)->fromLinear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert RGB to CIE Lab
|
||||||
|
* via xyz D65 to xyz D50
|
||||||
|
*
|
||||||
|
* @param RGB $rgb
|
||||||
|
* @return Lab
|
||||||
|
*/
|
||||||
|
public static function rgbViaXyzD65ViaXyzD50ToLab(RGB $rgb): Lab
|
||||||
|
{
|
||||||
|
return self::xyzD50ToLab(
|
||||||
|
self::xyzD65ToXyzD50(
|
||||||
|
self::linRgbToXyzD65($rgb)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert CIE Lab to RGB
|
||||||
|
* via xyz D50 to xyz D65
|
||||||
|
*
|
||||||
|
* @param Lab $lab
|
||||||
|
* @return RGB
|
||||||
|
*/
|
||||||
|
public static function labViaXyzD50ViaXyzD65ToRgb(Lab $lab): RGB
|
||||||
|
{
|
||||||
|
return self::xyzD65ToLinRgb(
|
||||||
|
self::xyzD50ToXyxD65(
|
||||||
|
self::labToXyzD50($lab)
|
||||||
|
)
|
||||||
|
)->fromLinear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param Lab $lab
|
||||||
|
* @return Lab
|
||||||
|
*/
|
||||||
|
public static function okLabViaXyzD65ViaXyzD50ToLab(Lab $lab): Lab
|
||||||
|
{
|
||||||
|
return self::xyzD50ToLab(
|
||||||
|
self::xyzD65ToXyzD50(
|
||||||
|
self::okLabToXyzD65($lab)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param Lab $lab
|
||||||
|
* @return Lab
|
||||||
|
*/
|
||||||
|
public static function labViaXyzD50ViaXyzD65ToOkLab(Lab $lab): Lab
|
||||||
|
{
|
||||||
|
return self::xyzD65ToOkLab(
|
||||||
|
self::xyzD50ToXyxD65(
|
||||||
|
self::labToXyzD50($lab)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: xyzD65 <-> xyzD50
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xyzD65 to xyzD50 whitepoint
|
||||||
|
*
|
||||||
|
* @param XYZ $xyz
|
||||||
|
* @return XYZ
|
||||||
|
*/
|
||||||
|
private static function xyzD65ToXyzD50(XYZ $xyz): XYZ
|
||||||
|
{
|
||||||
|
return XYZ::__constructFromArray(Math::multiplyMatrices(
|
||||||
|
a: [
|
||||||
|
[1.0479298208405488, 0.022946793341019088, -0.05019222954313557],
|
||||||
|
[0.029627815688159344, 0.990434484573249, -0.01707382502938514],
|
||||||
|
[-0.009243058152591178, 0.015055144896577895, 0.7518742899580008],
|
||||||
|
],
|
||||||
|
b: $xyz->returnAsArray(),
|
||||||
|
), whitepoint: 'D50');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xyzD50 to xyzD65 whitepoint
|
||||||
|
*
|
||||||
|
* @param XYZ $xyz
|
||||||
|
* @return XYZ
|
||||||
|
*/
|
||||||
|
private static function xyzD50ToXyxD65(XYZ $xyz): XYZ
|
||||||
|
{
|
||||||
|
return XYZ::__constructFromArray(Math::multiplyMatrices(
|
||||||
|
a: [
|
||||||
|
[0.9554734527042182, -0.023098536874261423, 0.0632593086610217],
|
||||||
|
[-0.028369706963208136, 1.0099954580058226, 0.021041398966943008],
|
||||||
|
[0.012314001688319899, -0.020507696433477912, 1.3303659366080753],
|
||||||
|
],
|
||||||
|
b: $xyz->returnAsArray()
|
||||||
|
), whitepoint: 'D65');
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: xyzD50 <-> Lab
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert xyzD50 to Lab (Cie)
|
||||||
|
*
|
||||||
|
* @param XYZ $xyz
|
||||||
|
* @return Lab
|
||||||
|
*/
|
||||||
|
private static function xyzD50ToLab(XYZ $xyz): Lab
|
||||||
|
{
|
||||||
|
$_xyz = $xyz->returnAsArray();
|
||||||
|
$d50 = [
|
||||||
|
0.3457 / 0.3585,
|
||||||
|
1.00000,
|
||||||
|
(1.0 - 0.3457 - 0.3585) / 0.3585,
|
||||||
|
];
|
||||||
|
|
||||||
|
$a = 216 / 24389;
|
||||||
|
$b = 24389 / 27;
|
||||||
|
|
||||||
|
$_xyz = array_map(
|
||||||
|
fn ($k, $v) => $v / $d50[$k],
|
||||||
|
array_keys($_xyz),
|
||||||
|
array_values($_xyz),
|
||||||
|
);
|
||||||
|
|
||||||
|
$f = array_map(
|
||||||
|
fn ($v) => (($v > $a) ?
|
||||||
|
pow($v, 1 / 3) :
|
||||||
|
(($b * $v + 16) / 116)
|
||||||
|
),
|
||||||
|
$_xyz,
|
||||||
|
);
|
||||||
|
|
||||||
|
return Lab::__constructFromArray([
|
||||||
|
(116 * $f[1]) - 16,
|
||||||
|
500 * ($f[0] - $f[1]),
|
||||||
|
200 * ($f[1] - $f[2]),
|
||||||
|
], colorspace: 'CIELab');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Lab (Cie) to xyz D50
|
||||||
|
*
|
||||||
|
* @param Lab $lab
|
||||||
|
* @return XYZ
|
||||||
|
*/
|
||||||
|
private static function labToXyzD50(Lab $lab): XYZ
|
||||||
|
{
|
||||||
|
$_lab = $lab->returnAsArray();
|
||||||
|
$a = 24389 / 27;
|
||||||
|
$b = 216 / 24389;
|
||||||
|
$f = [];
|
||||||
|
$f[1] = ($_lab[0] + 16) / 116;
|
||||||
|
$f[0] = $_lab[1] / 500 + $f[1];
|
||||||
|
$f[2] = $f[1] - $_lab[2] / 200;
|
||||||
|
$xyz = [
|
||||||
|
// x
|
||||||
|
pow($f[0], 3) > $b ?
|
||||||
|
pow($f[0], 3) :
|
||||||
|
(116 * $f[0] - 16) / $a,
|
||||||
|
// y
|
||||||
|
$_lab[0] > $a * $b ?
|
||||||
|
pow(($_lab[0] + 16) / 116, 3) :
|
||||||
|
$_lab[0] / $a,
|
||||||
|
// z
|
||||||
|
pow($f[2], 3) > $b ?
|
||||||
|
pow($f[2], 3) :
|
||||||
|
(116 * $f[2] - 16) / $a,
|
||||||
|
];
|
||||||
|
|
||||||
|
$d50 = [
|
||||||
|
0.3457 / 0.3585,
|
||||||
|
1.00000,
|
||||||
|
(1.0 - 0.3457 - 0.3585) / 0.3585,
|
||||||
|
];
|
||||||
|
|
||||||
|
return XYZ::__constructFromArray(
|
||||||
|
array_map(
|
||||||
|
fn ($k, $v) => $v * $d50[$k],
|
||||||
|
array_keys($xyz),
|
||||||
|
array_values($xyz),
|
||||||
|
),
|
||||||
|
whitepoint: 'D50'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: xyzD65 <-> (linear)RGB
|
||||||
|
|
||||||
|
/**
|
||||||
|
* convert linear RGB to xyz D65
|
||||||
|
* if rgb is not flagged linear, it will be auto converted
|
||||||
|
*
|
||||||
|
* @param RGB $rgb
|
||||||
|
* @return XYZ
|
||||||
|
*/
|
||||||
|
private static function linRgbToXyzD65(RGB $rgb): XYZ
|
||||||
|
{
|
||||||
|
// if not linear, convert to linear
|
||||||
|
if (!$rgb->linear) {
|
||||||
|
$rgb->toLinear();
|
||||||
|
}
|
||||||
|
return XYZ::__constructFromArray(Math::multiplyMatrices(
|
||||||
|
[
|
||||||
|
[0.41239079926595934, 0.357584339383878, 0.1804807884018343],
|
||||||
|
[0.21263900587151027, 0.715168678767756, 0.07219231536073371],
|
||||||
|
[0.01933081871559182, 0.11919477979462598, 0.9505321522496607],
|
||||||
|
],
|
||||||
|
$rgb->returnAsArray()
|
||||||
|
), whitepoint: 'D65');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert xyz D65 to linear RGB
|
||||||
|
*
|
||||||
|
* @param XYZ $xyz
|
||||||
|
* @return RGB
|
||||||
|
*/
|
||||||
|
private static function xyzD65ToLinRgb(XYZ $xyz): RGB
|
||||||
|
{
|
||||||
|
// xyz D65 to linrgb
|
||||||
|
return RGB::__constructFromArray(Math::multiplyMatrices(
|
||||||
|
a : [
|
||||||
|
[ 3.2409699419045226, -1.537383177570094, -0.4986107602930034 ],
|
||||||
|
[ -0.9692436362808796, 1.8759675015077202, 0.04155505740717559 ],
|
||||||
|
[ 0.05563007969699366, -0.20397695888897652, 1.0569715142428786 ],
|
||||||
|
],
|
||||||
|
b : $xyz->returnAsArray()
|
||||||
|
), linear: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: xyzD65 <-> OkLab
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xyz D65 to OkLab
|
||||||
|
*
|
||||||
|
* @param XYZ $xyz
|
||||||
|
* @return Lab
|
||||||
|
*/
|
||||||
|
private static function xyzD65ToOkLab(XYZ $xyz): Lab
|
||||||
|
{
|
||||||
|
return Lab::__constructFromArray(Math::multiplyMatrices(
|
||||||
|
[
|
||||||
|
[0.2104542553, 0.7936177850, -0.0040720468],
|
||||||
|
[1.9779984951, -2.4285922050, 0.4505937099],
|
||||||
|
[0.0259040371, 0.7827717662, -0.8086757660],
|
||||||
|
],
|
||||||
|
array_map(
|
||||||
|
callback: fn ($v) => pow($v, 1 / 3),
|
||||||
|
array: Math::multiplyMatrices(
|
||||||
|
a: [
|
||||||
|
[0.8190224432164319, 0.3619062562801221, -0.12887378261216414],
|
||||||
|
[0.0329836671980271, 0.9292868468965546, 0.03614466816999844],
|
||||||
|
[0.048177199566046255, 0.26423952494422764, 0.6335478258136937],
|
||||||
|
],
|
||||||
|
b: $xyz->returnAsArray(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
), colorspace: 'OkLab');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xyz D65 to OkLab
|
||||||
|
*
|
||||||
|
* @param Lab $lab
|
||||||
|
* @return XYZ
|
||||||
|
*/
|
||||||
|
private static function okLabToXyzD65(Lab $lab): XYZ
|
||||||
|
{
|
||||||
|
return XYZ::__constructFromArray(Math::multiplyMatrices(
|
||||||
|
a: [
|
||||||
|
[1.2268798733741557, -0.5578149965554813, 0.28139105017721583],
|
||||||
|
[-0.04057576262431372, 1.1122868293970594, -0.07171106666151701],
|
||||||
|
[-0.07637294974672142, -0.4214933239627914, 1.5869240244272418],
|
||||||
|
],
|
||||||
|
b: array_map(
|
||||||
|
callback: fn ($v) => is_numeric($v) ? $v ** 3 : 0,
|
||||||
|
array: Math::multiplyMatrices(
|
||||||
|
a: [
|
||||||
|
[0.99999999845051981432, 0.39633779217376785678, 0.21580375806075880339],
|
||||||
|
[1.0000000088817607767, -0.1055613423236563494, -0.063854174771705903402],
|
||||||
|
[1.0000000546724109177, -0.089484182094965759684, -1.2914855378640917399],
|
||||||
|
],
|
||||||
|
// Divide $lightness by 100 to convert from CSS OkLab
|
||||||
|
b: $lab->returnAsArray(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
), whitepoint: 'D65');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
1108
www/lib/CoreLibs/Convert/Color/Color.php
Normal file
1108
www/lib/CoreLibs/Convert/Color/Color.php
Normal file
File diff suppressed because it is too large
Load Diff
164
www/lib/CoreLibs/Convert/Color/Coordinates/HSB.php
Normal file
164
www/lib/CoreLibs/Convert/Color/Coordinates/HSB.php
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUTHOR: Clemens Schwaighofer
|
||||||
|
* CREATED: 2024/11/11
|
||||||
|
* DESCRIPTION:
|
||||||
|
* Color Coordinate: HSB/HSV
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert\Color\Coordinates;
|
||||||
|
|
||||||
|
class HSB
|
||||||
|
{
|
||||||
|
/** @var array<string> allowed colorspaces */
|
||||||
|
private const COLORSPACES = ['sRGB'];
|
||||||
|
|
||||||
|
/** @var float hue */
|
||||||
|
private float $H = 0.0;
|
||||||
|
/** @var float saturation */
|
||||||
|
private float $S = 0.0;
|
||||||
|
/** @var float brightness / value */
|
||||||
|
private float $B = 0.0;
|
||||||
|
|
||||||
|
/** @var string color space: either ok or cie */
|
||||||
|
private string $colorspace = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HSB (HSV) color coordinates
|
||||||
|
* Hue/Saturation/Brightness or Value
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set from array
|
||||||
|
* where 0: Hue, 1: Saturation, 2: Brightness
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @param string $colorspace [default=sRGB]
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function __constructFromArray(array $colors, string $colorspace = 'sRGB'): self
|
||||||
|
{
|
||||||
|
return (new HSB())->setColorspace($colorspace)->setFromArray($colors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param float $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, float $value): void
|
||||||
|
{
|
||||||
|
$name = strtoupper($name);
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
switch ($name) {
|
||||||
|
case 'H':
|
||||||
|
if ($value == 360) {
|
||||||
|
$value = 0;
|
||||||
|
}
|
||||||
|
if ($value < 0 || $value > 359) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for hue is not in the range of 0 to 359',
|
||||||
|
1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'S':
|
||||||
|
if ($value < 0 || $value > 100) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for saturation is not in the range of 0 to 100',
|
||||||
|
2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'B':
|
||||||
|
if ($value < 0 || $value > 100) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for brightness is not in the range of 0 to 100',
|
||||||
|
3
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function __get(string $name): float
|
||||||
|
{
|
||||||
|
$name = strtoupper($name);
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
return $this->$name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the colorspace
|
||||||
|
*
|
||||||
|
* @param string $colorspace
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setColorspace(string $colorspace): self
|
||||||
|
{
|
||||||
|
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||||
|
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||||
|
}
|
||||||
|
$this->colorspace = $colorspace;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the color as array
|
||||||
|
* where 0: Hue, 1: Saturation, 2: Brightness
|
||||||
|
*
|
||||||
|
* @return array{0:float,1:float,2:float}
|
||||||
|
*/
|
||||||
|
public function returnAsArray(): array
|
||||||
|
{
|
||||||
|
return [$this->H, $this->S, $this->B];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color as array
|
||||||
|
* where 0: Hue, 1: Saturation, 2: Brightness
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setFromArray(array $colors): self
|
||||||
|
{
|
||||||
|
$this->__set('H', $colors[0]);
|
||||||
|
$this->__set('S', $colors[1]);
|
||||||
|
$this->__set('B', $colors[2]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* no hsb in css
|
||||||
|
*
|
||||||
|
* @param float|string|null $opacity
|
||||||
|
* @return string
|
||||||
|
* @throws \ErrorException
|
||||||
|
*/
|
||||||
|
public function toCssString(null|float|string $opacity = null): string
|
||||||
|
{
|
||||||
|
throw new \ErrorException('HSB is not available as CSS color string', 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
171
www/lib/CoreLibs/Convert/Color/Coordinates/HSL.php
Normal file
171
www/lib/CoreLibs/Convert/Color/Coordinates/HSL.php
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUTHOR: Clemens Schwaighofer
|
||||||
|
* CREATED: 2024/11/11
|
||||||
|
* DESCRIPTION:
|
||||||
|
* Color Coordinate: HSL
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert\Color\Coordinates;
|
||||||
|
|
||||||
|
use CoreLibs\Convert\Color\Stringify;
|
||||||
|
|
||||||
|
class HSL
|
||||||
|
{
|
||||||
|
/** @var array<string> allowed colorspaces */
|
||||||
|
private const COLORSPACES = ['sRGB'];
|
||||||
|
|
||||||
|
/** @var float hue */
|
||||||
|
private float $H = 0.0;
|
||||||
|
/** @var float saturation */
|
||||||
|
private float $S = 0.0;
|
||||||
|
/** @var float lightness (luminance) */
|
||||||
|
private float $L = 0.0;
|
||||||
|
|
||||||
|
/** @var string color space: either ok or cie */
|
||||||
|
private string $colorspace = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Color Coordinate HSL
|
||||||
|
* Hue/Saturation/Lightness
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set from array
|
||||||
|
* where 0: Hue, 1: Saturation, 2: Lightness
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @param string $colorspace [default=sRGB]
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function __constructFromArray(array $colors, string $colorspace = 'sRGB'): self
|
||||||
|
{
|
||||||
|
return (new HSL())->setColorspace($colorspace)->setFromArray($colors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param float $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, float $value): void
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
switch ($name) {
|
||||||
|
case 'H':
|
||||||
|
if ($value == 360) {
|
||||||
|
$value = 0;
|
||||||
|
}
|
||||||
|
if ($value < 0 || $value > 359) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for hue is not in the range of 0 to 359',
|
||||||
|
1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'S':
|
||||||
|
if ($value < 0 || $value > 100) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for saturation is not in the range of 0 to 100',
|
||||||
|
2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'L':
|
||||||
|
if ($value < 0 || $value > 100) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for luminance is not in the range of 0 to 100',
|
||||||
|
3
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function __get(string $name): float
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
return $this->$name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the colorspace
|
||||||
|
*
|
||||||
|
* @param string $colorspace
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setColorspace(string $colorspace): self
|
||||||
|
{
|
||||||
|
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||||
|
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||||
|
}
|
||||||
|
$this->colorspace = $colorspace;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the color as array
|
||||||
|
* where 0: Hue, 1: Saturation, 2: Lightness
|
||||||
|
*
|
||||||
|
* @return array{0:float,1:float,2:float}
|
||||||
|
*/
|
||||||
|
public function returnAsArray(): array
|
||||||
|
{
|
||||||
|
return [$this->H, $this->S, $this->L];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color as array
|
||||||
|
* where 0: Hue, 1: Saturation, 2: Lightness
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setFromArray(array $colors): self
|
||||||
|
{
|
||||||
|
$this->__set('H', $colors[0]);
|
||||||
|
$this->__set('S', $colors[1]);
|
||||||
|
$this->__set('L', $colors[2]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* convert to css string with optional opacityt
|
||||||
|
*
|
||||||
|
* @param float|string|null $opacity
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toCssString(null|float|string $opacity = null): string
|
||||||
|
{
|
||||||
|
$string = 'hsl('
|
||||||
|
. $this->H
|
||||||
|
. ' '
|
||||||
|
. $this->S
|
||||||
|
. ' '
|
||||||
|
. $this->L
|
||||||
|
. Stringify::setOpacity($opacity)
|
||||||
|
. ')';
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
171
www/lib/CoreLibs/Convert/Color/Coordinates/HWB.php
Normal file
171
www/lib/CoreLibs/Convert/Color/Coordinates/HWB.php
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUTHOR: Clemens Schwaighofer
|
||||||
|
* CREATED: 2024/11/11
|
||||||
|
* DESCRIPTION:
|
||||||
|
* Color Coordinate: HWB
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert\Color\Coordinates;
|
||||||
|
|
||||||
|
use CoreLibs\Convert\Color\Stringify;
|
||||||
|
|
||||||
|
class HWB
|
||||||
|
{
|
||||||
|
/** @var array<string> allowed colorspaces */
|
||||||
|
private const COLORSPACES = ['sRGB'];
|
||||||
|
|
||||||
|
/** @var float Hue */
|
||||||
|
private float $H = 0.0;
|
||||||
|
/** @var float Whiteness */
|
||||||
|
private float $W = 0.0;
|
||||||
|
/** @var float Blackness */
|
||||||
|
private float $B = 0.0;
|
||||||
|
|
||||||
|
/** @var string color space: either ok or cie */
|
||||||
|
private string $colorspace = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Color Coordinate: HWB
|
||||||
|
* Hue/Whiteness/Blackness
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set from array
|
||||||
|
* where 0: Hue, 1: Whiteness, 2: Blackness
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @param string $colorspace [default=sRGB]
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function __constructFromArray(array $colors, string $colorspace = 'sRGB'): self
|
||||||
|
{
|
||||||
|
return (new HWB())->setColorspace($colorspace)->setFromArray($colors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param float $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, float $value): void
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
switch ($name) {
|
||||||
|
case 'H':
|
||||||
|
if ($value == 360) {
|
||||||
|
$value = 0;
|
||||||
|
}
|
||||||
|
if ($value < 0 || $value > 360) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for hue is not in the range of 0 to 360',
|
||||||
|
1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'W':
|
||||||
|
if ($value < 0 || $value > 100) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for saturation is not in the range of 0 to 100',
|
||||||
|
2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'B':
|
||||||
|
if ($value < 0 || $value > 100) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for luminance is not in the range of 0 to 100',
|
||||||
|
3
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function __get(string $name): float
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
return $this->$name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the colorspace
|
||||||
|
*
|
||||||
|
* @param string $colorspace
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setColorspace(string $colorspace): self
|
||||||
|
{
|
||||||
|
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||||
|
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||||
|
}
|
||||||
|
$this->colorspace = $colorspace;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the color as array
|
||||||
|
* where 0: Hue, 1: Whiteness, 2: Blackness
|
||||||
|
*
|
||||||
|
* @return array{0:float,1:float,2:float}
|
||||||
|
*/
|
||||||
|
public function returnAsArray(): array
|
||||||
|
{
|
||||||
|
return [$this->H, $this->W, $this->B];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color as array
|
||||||
|
* where 0: Hue, 1: Whiteness, 2: Blackness
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setFromArray(array $colors): self
|
||||||
|
{
|
||||||
|
$this->__set('H', $colors[0]);
|
||||||
|
$this->__set('W', $colors[1]);
|
||||||
|
$this->__set('B', $colors[2]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* convert to css string with optional opacityt
|
||||||
|
*
|
||||||
|
* @param float|string|null $opacity
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toCssString(null|float|string $opacity = null): string
|
||||||
|
{
|
||||||
|
$string = 'hwb('
|
||||||
|
. $this->H
|
||||||
|
. ' '
|
||||||
|
. $this->W
|
||||||
|
. ' '
|
||||||
|
. $this->B
|
||||||
|
. Stringify::setOpacity($opacity)
|
||||||
|
. ')';
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
206
www/lib/CoreLibs/Convert/Color/Coordinates/LCH.php
Normal file
206
www/lib/CoreLibs/Convert/Color/Coordinates/LCH.php
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUTHOR: Clemens Schwaighofer
|
||||||
|
* CREATED: 2024/11/11
|
||||||
|
* DESCRIPTION:
|
||||||
|
* Color Coordinate: Lch
|
||||||
|
* for oklch or cie
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert\Color\Coordinates;
|
||||||
|
|
||||||
|
use CoreLibs\Convert\Color\Stringify;
|
||||||
|
|
||||||
|
class LCH
|
||||||
|
{
|
||||||
|
/** @var array<string> allowed colorspaces */
|
||||||
|
private const COLORSPACES = ['OkLab', 'CIELab'];
|
||||||
|
|
||||||
|
/** @var float Lightness/Luminance
|
||||||
|
* CIE: 0 to 100
|
||||||
|
* OKlch: 0.0 to 1.0
|
||||||
|
* BOTH: 0% to 100%
|
||||||
|
*/
|
||||||
|
private float $L = 0.0;
|
||||||
|
/** @var float Chroma
|
||||||
|
* CIE: 0 to 150, cannot be more than 230
|
||||||
|
* OkLch: 0 to 0.4, does not exceed 0.5
|
||||||
|
* BOTH: 0% to 100% (0 to 150, 0 to 0.4)
|
||||||
|
*/
|
||||||
|
private float $C = 0.0;
|
||||||
|
/** @var float Hue
|
||||||
|
* 0 to 360 deg
|
||||||
|
*/
|
||||||
|
private float $H = 0.0;
|
||||||
|
|
||||||
|
/** @var string color space: either ok or cie */
|
||||||
|
private string $colorspace = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Color Coordinate Lch
|
||||||
|
* for oklch
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set from array
|
||||||
|
* where 0: Lightness, 1: Chroma, 2: Hue
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @param string $colorspace
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function __constructFromArray(array $colors, string $colorspace): self
|
||||||
|
{
|
||||||
|
return (new LCH())->setColorspace($colorspace)->setFromArray($colors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param float $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, float $value): void
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
switch ($name) {
|
||||||
|
// case 'L':
|
||||||
|
// if ($this->colorspace == 'cie' && ($value < 0 || $value > 100)) {
|
||||||
|
// throw new \LengthException(
|
||||||
|
// 'Argument value ' . $value . ' for lightness is not in the range of '
|
||||||
|
// . '0 to 100',
|
||||||
|
// 3
|
||||||
|
// );
|
||||||
|
// } elseif ($this->colorspace == 'ok' && ($value < 0 || $value > 1)) {
|
||||||
|
// throw new \LengthException(
|
||||||
|
// 'Argument value ' . $value . ' for lightness is not in the range of '
|
||||||
|
// . '0 to 1',
|
||||||
|
// 3
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// break;
|
||||||
|
// case 'c':
|
||||||
|
// if ($this->colorspace == 'cie' && ($value < 0 || $value > 230)) {
|
||||||
|
// throw new \LengthException(
|
||||||
|
// 'Argument value ' . $value . ' for chroma is not in the range of '
|
||||||
|
// . '0 to 230 with normal upper limit of 150',
|
||||||
|
// 3
|
||||||
|
// );
|
||||||
|
// } elseif ($this->colorspace == 'ok' && ($value < 0 || $value > 0.5)) {
|
||||||
|
// throw new \LengthException(
|
||||||
|
// 'Argument value ' . $value . ' for chroma is not in the range of '
|
||||||
|
// . '0 to 0.5 with normal upper limit of 0.5',
|
||||||
|
// 3
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// break;
|
||||||
|
case 'h':
|
||||||
|
if ($value == 360) {
|
||||||
|
$value = 0;
|
||||||
|
}
|
||||||
|
if ($value < 0 || $value > 360) {
|
||||||
|
throw new \LengthException(
|
||||||
|
'Argument value ' . $value . ' for lightness is not in the range of 0 to 360',
|
||||||
|
1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function __get(string $name): float
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
return $this->$name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the colorspace
|
||||||
|
*
|
||||||
|
* @param string $colorspace
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setColorspace(string $colorspace): self
|
||||||
|
{
|
||||||
|
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||||
|
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||||
|
}
|
||||||
|
$this->colorspace = $colorspace;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the color as array
|
||||||
|
* where 0: Lightness, 1: Chroma, 2: Hue
|
||||||
|
*
|
||||||
|
* @return array{0:float,1:float,2:float}
|
||||||
|
*/
|
||||||
|
public function returnAsArray(): array
|
||||||
|
{
|
||||||
|
return [$this->L, $this->C, $this->H];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color as array
|
||||||
|
* where 0: Lightness, 1: Chroma, 2: Hue
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setFromArray(array $colors): self
|
||||||
|
{
|
||||||
|
$this->__set('L', $colors[0]);
|
||||||
|
$this->__set('C', $colors[1]);
|
||||||
|
$this->__set('H', $colors[2]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert into css string with optional opacity
|
||||||
|
*
|
||||||
|
* @param null|float|string|null $opacity
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toCssString(null|float|string $opacity = null): string
|
||||||
|
{
|
||||||
|
$string = '';
|
||||||
|
switch ($this->colorspace) {
|
||||||
|
case 'CIELab':
|
||||||
|
$string = 'lch';
|
||||||
|
break;
|
||||||
|
case 'OkLab':
|
||||||
|
$string = 'oklch';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$string .= '('
|
||||||
|
. $this->L
|
||||||
|
. ' '
|
||||||
|
. $this->c
|
||||||
|
. ' '
|
||||||
|
. $this->h
|
||||||
|
. Stringify::setOpacity($opacity)
|
||||||
|
. ');';
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
194
www/lib/CoreLibs/Convert/Color/Coordinates/Lab.php
Normal file
194
www/lib/CoreLibs/Convert/Color/Coordinates/Lab.php
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUTHOR: Clemens Schwaighofer
|
||||||
|
* CREATED: 2024/11/11
|
||||||
|
* DESCRIPTION:
|
||||||
|
* Color Coordinate: Lab
|
||||||
|
* for oklab or cie
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert\Color\Coordinates;
|
||||||
|
|
||||||
|
use CoreLibs\Convert\Color\Stringify;
|
||||||
|
|
||||||
|
class Lab
|
||||||
|
{
|
||||||
|
/** @var array<string> allowed colorspaces */
|
||||||
|
private const COLORSPACES = ['OkLab', 'CIELab'];
|
||||||
|
|
||||||
|
/** @var float lightness/luminance
|
||||||
|
* CIE: 0f to 100f
|
||||||
|
* OKlab: 0.0 to 1.0
|
||||||
|
* BOTH: 0% to 100%
|
||||||
|
*/
|
||||||
|
private float $L = 0.0;
|
||||||
|
/** @var float a axis distance
|
||||||
|
* CIE: -125 to 125, cannot be more than +/- 160
|
||||||
|
* OKlab: -0.4 to 0.4, cannot exceed +/- 0.5
|
||||||
|
* BOTH: -100% to 100% (+/-125 or 0.4)
|
||||||
|
*/
|
||||||
|
private float $a = 0.0;
|
||||||
|
/** @var float b axis distance
|
||||||
|
* CIE: -125 to 125, cannot be more than +/- 160
|
||||||
|
* OKlab: -0.4 to 0.4, cannot exceed +/- 0.5
|
||||||
|
* BOTH: -100% to 100% (+/-125 or 0.4)
|
||||||
|
*/
|
||||||
|
private float $b = 0.0;
|
||||||
|
|
||||||
|
/** @var string color space: either ok or cie */
|
||||||
|
private string $colorspace = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Color Coordinate: Lab
|
||||||
|
* for oklab or cie
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set from array
|
||||||
|
* where 0: Lightness, 1: a, 2: b
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $rgb
|
||||||
|
* @param string $colorspace
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function __constructFromArray(array $colors, string $colorspace): self
|
||||||
|
{
|
||||||
|
return (new Lab())->setColorspace($colorspace)->setFromArray($colors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param float $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, float $value): void
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
// switch ($name) {
|
||||||
|
// case 'L':
|
||||||
|
// if ($value == 360) {
|
||||||
|
// $value = 0;
|
||||||
|
// }
|
||||||
|
// if ($value < 0 || $value > 360) {
|
||||||
|
// throw new \LengthException(
|
||||||
|
// 'Argument value ' . $value . ' for lightness is not in the range of 0 to 360',
|
||||||
|
// 1
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// break;
|
||||||
|
// case 'a':
|
||||||
|
// if ($value < 0 || $value > 100) {
|
||||||
|
// throw new \LengthException(
|
||||||
|
// 'Argument value ' . $value . ' for a is not in the range of 0 to 100',
|
||||||
|
// 2
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// break;
|
||||||
|
// case 'b':
|
||||||
|
// if ($value < 0 || $value > 100) {
|
||||||
|
// throw new \LengthException(
|
||||||
|
// 'Argument value ' . $value . ' for b is not in the range of 0 to 100',
|
||||||
|
// 3
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function __get(string $name): float
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
return $this->$name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the colorspace
|
||||||
|
*
|
||||||
|
* @param string $colorspace
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setColorspace(string $colorspace): self
|
||||||
|
{
|
||||||
|
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||||
|
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||||
|
}
|
||||||
|
$this->colorspace = $colorspace;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the color as array
|
||||||
|
* where 0: Lightness, 1: a, 2: b
|
||||||
|
*
|
||||||
|
* @return array{0:float,1:float,2:float}
|
||||||
|
*/
|
||||||
|
public function returnAsArray(): array
|
||||||
|
{
|
||||||
|
return [$this->L, $this->a, $this->b];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color as array
|
||||||
|
* where 0: Lightness, 1: a, 2: b
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setFromArray(array $colors): self
|
||||||
|
{
|
||||||
|
$this->__set('L', $colors[0]);
|
||||||
|
$this->__set('a', $colors[1]);
|
||||||
|
$this->__set('b', $colors[2]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert into css string with optional opacity
|
||||||
|
*
|
||||||
|
* @param null|float|string|null $opacity
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toCssString(null|float|string $opacity = null): string
|
||||||
|
{
|
||||||
|
$string = '';
|
||||||
|
switch ($this->colorspace) {
|
||||||
|
case 'CIELab':
|
||||||
|
$string = 'lab';
|
||||||
|
break;
|
||||||
|
case 'OkLab':
|
||||||
|
$string = 'oklab';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$string .= '('
|
||||||
|
. $this->L
|
||||||
|
. ' '
|
||||||
|
. $this->a
|
||||||
|
. ' '
|
||||||
|
. $this->b
|
||||||
|
. Stringify::setOpacity($opacity)
|
||||||
|
. ');';
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
313
www/lib/CoreLibs/Convert/Color/Coordinates/RGB.php
Normal file
313
www/lib/CoreLibs/Convert/Color/Coordinates/RGB.php
Normal file
@@ -0,0 +1,313 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUTHOR: Clemens Schwaighofer
|
||||||
|
* CREATED: 2024/11/11
|
||||||
|
* DESCRIPTION:
|
||||||
|
* Color Coordinate: RGB
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert\Color\Coordinates;
|
||||||
|
|
||||||
|
use CoreLibs\Convert\Color\Stringify;
|
||||||
|
|
||||||
|
class RGB
|
||||||
|
{
|
||||||
|
/** @var array<string> allowed colorspaces */
|
||||||
|
private const COLORSPACES = ['sRGB'];
|
||||||
|
|
||||||
|
/** @var float red 0 to 255 or 0.0f to 1.0f for linear RGB */
|
||||||
|
private float $R = 0.0;
|
||||||
|
/** @var float green 0 to 255 or 0.0f to 1.0f for linear RGB */
|
||||||
|
private float $G = 0.0;
|
||||||
|
/** @var float blue 0 to 255 or 0.0f to 1.0f for linear RGB */
|
||||||
|
private float $B = 0.0;
|
||||||
|
|
||||||
|
/** @var string color space: either ok or cie */
|
||||||
|
private string $colorspace = '';
|
||||||
|
|
||||||
|
/** @var bool set if this is linear */
|
||||||
|
private bool $linear = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Color Coordinate RGB
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set from array
|
||||||
|
* where 0: Red, 1: Green, 2: Blue
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @param string $colorspace [default=sRGB]
|
||||||
|
* @param bool $linear [default=false]
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function __constructFromArray(array $colors, string $colorspace = 'sRGB', bool $linear = false): self
|
||||||
|
{
|
||||||
|
return (new RGB())->setColorspace($colorspace)->flagLinear($linear)->setFromArray($colors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @param string $hex_string
|
||||||
|
* @param string $colorspace
|
||||||
|
* @param bool $linear
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function __constructFromHexString(
|
||||||
|
string $hex_string,
|
||||||
|
string $colorspace = 'sRGB',
|
||||||
|
bool $linear = false
|
||||||
|
): self {
|
||||||
|
return (new RGB())->setColorspace($colorspace)->flagLinear($linear)->setFromHex($hex_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param float $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, float $value): void
|
||||||
|
{
|
||||||
|
// do not allow setting linear from outside
|
||||||
|
if ($name == 'linear') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
// if not linear
|
||||||
|
if (!$this->linear && ($value < 0 || $value > 255)) {
|
||||||
|
throw new \LengthException('Argument value ' . $value . ' for color ' . $name
|
||||||
|
. ' is not in the range of 0 to 255', 1);
|
||||||
|
} elseif ($this->linear && ($value < -10E10 || $value > 1)) {
|
||||||
|
// not allow very very small negative numbers
|
||||||
|
throw new \LengthException('Argument value ' . $value . ' for color ' . $name
|
||||||
|
. ' is not in the range of 0 to 1 for linear rgb', 1);
|
||||||
|
}
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return float|bool
|
||||||
|
*/
|
||||||
|
public function __get(string $name): float|bool
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
return $this->$name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the colorspace
|
||||||
|
*
|
||||||
|
* @param string $colorspace
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setColorspace(string $colorspace): self
|
||||||
|
{
|
||||||
|
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||||
|
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||||
|
}
|
||||||
|
$this->colorspace = $colorspace;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the color as array
|
||||||
|
* where 0: Red, 1: Green, 2: Blue
|
||||||
|
*
|
||||||
|
* @return array{0:float,1:float,2:float}
|
||||||
|
*/
|
||||||
|
public function returnAsArray(): array
|
||||||
|
{
|
||||||
|
return [$this->R, $this->G, $this->B];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color as array
|
||||||
|
* where 0: Red, 1: Green, 2: Blue
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setFromArray(array $colors): self
|
||||||
|
{
|
||||||
|
$this->__set('R', $colors[0]);
|
||||||
|
$this->__set('G', $colors[1]);
|
||||||
|
$this->__set('B', $colors[2]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return current set RGB as hex string. with or without # prefix
|
||||||
|
*
|
||||||
|
* @param bool $hex_prefix
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function returnAsHex(bool $hex_prefix = true): string
|
||||||
|
{
|
||||||
|
// prefix
|
||||||
|
$hex_color = '';
|
||||||
|
if ($hex_prefix === true) {
|
||||||
|
$hex_color = '#';
|
||||||
|
}
|
||||||
|
// convert if in linear
|
||||||
|
if ($this->linear) {
|
||||||
|
$this->fromLinear();
|
||||||
|
}
|
||||||
|
foreach ($this->returnAsArray() as $color) {
|
||||||
|
$hex_color .= str_pad(dechex((int)$color), 2, '0', STR_PAD_LEFT);
|
||||||
|
}
|
||||||
|
return $hex_color;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set colors RGB from hex string
|
||||||
|
*
|
||||||
|
* @param string $hex_string
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setFromHex(string $hex_string): self
|
||||||
|
{
|
||||||
|
$hex_string = preg_replace("/[^0-9A-Fa-f]/", '', $hex_string); // Gets a proper hex string
|
||||||
|
if (!is_string($hex_string)) {
|
||||||
|
throw new \InvalidArgumentException('hex_string argument cannot be empty', 1);
|
||||||
|
}
|
||||||
|
$rgbArray = [];
|
||||||
|
if (strlen($hex_string) == 6) {
|
||||||
|
// If a proper hex code, convert using bitwise operation.
|
||||||
|
// No overhead... faster
|
||||||
|
$colorVal = hexdec($hex_string);
|
||||||
|
$rgbArray = [
|
||||||
|
0xFF & ($colorVal >> 0x10),
|
||||||
|
0xFF & ($colorVal >> 0x8),
|
||||||
|
0xFF & $colorVal
|
||||||
|
];
|
||||||
|
} elseif (strlen($hex_string) == 3) {
|
||||||
|
// If shorthand notation, need some string manipulations
|
||||||
|
$rgbArray = [
|
||||||
|
hexdec(str_repeat(substr($hex_string, 0, 1), 2)),
|
||||||
|
hexdec(str_repeat(substr($hex_string, 1, 1), 2)),
|
||||||
|
hexdec(str_repeat(substr($hex_string, 2, 1), 2))
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
// Invalid hex color code
|
||||||
|
throw new \UnexpectedValueException('Invalid hex_string: ' . $hex_string, 2);
|
||||||
|
}
|
||||||
|
return $this->setFromArray($rgbArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set as linear
|
||||||
|
* can be used as chain call on create if input is linear RGB
|
||||||
|
* RGB::__construct**(...)->flagLinear();
|
||||||
|
* as it returns self
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function flagLinear(bool $linear): self
|
||||||
|
{
|
||||||
|
$this->linear = $linear;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Both function source:
|
||||||
|
* https://bottosson.github.io/posts/colorwrong/#what-can-we-do%3F
|
||||||
|
* but reverse f: fromLinear and f_inv for toLinear
|
||||||
|
* Code copied from here:
|
||||||
|
* https://stackoverflow.com/a/12894053
|
||||||
|
*
|
||||||
|
* converts RGB to linear
|
||||||
|
* We come from 0-255 so we need to divide by 255
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function toLinear(): self
|
||||||
|
{
|
||||||
|
$this->flagLinear(true)->setFromArray(array_map(
|
||||||
|
callback: function (int|float $v) {
|
||||||
|
$v = (float)($v / 255);
|
||||||
|
$abs = abs($v);
|
||||||
|
$sign = ($v < 0) ? -1 : 1;
|
||||||
|
return (float)(
|
||||||
|
$abs <= 0.04045 ?
|
||||||
|
$v / 12.92 :
|
||||||
|
$sign * pow(($abs + 0.055) / 1.055, 2.4)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
array: $this->returnAsArray(),
|
||||||
|
));
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* convert back to normal sRGB from linear RGB
|
||||||
|
* we go to 0-255 rgb so we multiply by 255
|
||||||
|
*
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function fromLinear(): self
|
||||||
|
{
|
||||||
|
$this->flagLinear(false)->setFromArray(array_map(
|
||||||
|
callback: function (int|float $v) {
|
||||||
|
$abs = abs($v);
|
||||||
|
$sign = ($v < 0) ? -1 : 1;
|
||||||
|
// during reverse in some situations the values can become negative in very small ways
|
||||||
|
// like -...E16 and ...E17
|
||||||
|
return ($ret = (float)(255 * (
|
||||||
|
$abs <= 0.0031308 ?
|
||||||
|
$v * 12.92 :
|
||||||
|
$sign * (1.055 * pow($abs, 1.0 / 2.4) - 0.055)
|
||||||
|
))) < 0 ? 0 : $ret;
|
||||||
|
},
|
||||||
|
array: $this->returnAsArray(),
|
||||||
|
));
|
||||||
|
// $this->linear = false;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* convert to css string with optional opacity
|
||||||
|
* Note: if this is a linear RGB, the data will converted during this operation and the converted back
|
||||||
|
*
|
||||||
|
* @param float|string|null $opacity
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toCssString(null|float|string $opacity = null): string
|
||||||
|
{
|
||||||
|
// if we are in linear mode, convert to normal mode temporary
|
||||||
|
$was_linear = false;
|
||||||
|
if ($this->linear) {
|
||||||
|
$this->fromLinear();
|
||||||
|
$was_linear = true;
|
||||||
|
}
|
||||||
|
$string = 'rgb('
|
||||||
|
. (int)round($this->R, 0)
|
||||||
|
. ' '
|
||||||
|
. (int)round($this->G, 0)
|
||||||
|
. ' '
|
||||||
|
. (int)round($this->B, 0)
|
||||||
|
. Stringify::setOpacity($opacity)
|
||||||
|
. ')';
|
||||||
|
if ($was_linear) {
|
||||||
|
$this->toLinear();
|
||||||
|
}
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
162
www/lib/CoreLibs/Convert/Color/Coordinates/XYZ.php
Normal file
162
www/lib/CoreLibs/Convert/Color/Coordinates/XYZ.php
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUTHOR: Clemens Schwaighofer
|
||||||
|
* CREATED: 2024/11/11
|
||||||
|
* DESCRIPTION:
|
||||||
|
* Color Coordinate: XYZ (Cie) (colorspace CIEXYZ)
|
||||||
|
* Note, this is only for the D50 & D65 whitepoint conversion
|
||||||
|
* https://en.wikipedia.org/wiki/CIE_1931_color_space#Construction_of_the_CIE_XYZ_color_space_from_the_Wright%E2%80%93Guild_data
|
||||||
|
* https://en.wikipedia.org/wiki/Standard_illuminant#Illuminant_series_D
|
||||||
|
* https://en.wikipedia.org/wiki/Standard_illuminant#D65_values
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert\Color\Coordinates;
|
||||||
|
|
||||||
|
class XYZ
|
||||||
|
{
|
||||||
|
/** @var array<string> allowed colorspaces */
|
||||||
|
private const COLORSPACES = ['CIEXYZ'];
|
||||||
|
/** @var array<string> allowed whitepoints
|
||||||
|
* D50: ICC profile PCS (horizon light) <-> CieLab
|
||||||
|
* D65: RGB color space (noon) <-> linear RGB
|
||||||
|
*/
|
||||||
|
private const ILLUMINANT = ['D50', 'D65'];
|
||||||
|
|
||||||
|
/** @var float X coordinate */
|
||||||
|
private float $X = 0.0;
|
||||||
|
/** @var float Y coordinate (Luminance) */
|
||||||
|
private float $Y = 0.0;
|
||||||
|
/** @var float Z coordinate (blue) */
|
||||||
|
private float $Z = 0.0;
|
||||||
|
|
||||||
|
/** @var string color space: either ok or cie */
|
||||||
|
private string $colorspace = '';
|
||||||
|
|
||||||
|
private string $whitepoint = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Color Coordinate Lch
|
||||||
|
* for oklch
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set from array
|
||||||
|
* where 0: X, 1: Y, 2: Z
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @param string $colorspace [default=CIEXYZ]
|
||||||
|
* @param string $whitepoint [default=''] only D65 or D50 allowed
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public static function __constructFromArray(
|
||||||
|
array $colors,
|
||||||
|
string $colorspace = 'CIEXYZ',
|
||||||
|
string $whitepoint = ''
|
||||||
|
): self {
|
||||||
|
return (new XYZ())
|
||||||
|
->setColorspace($colorspace)
|
||||||
|
->setWhitepoint($whitepoint)
|
||||||
|
->setFromArray($colors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param float $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, float $value): void
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
// if ($value < 0 || $value > 255) {
|
||||||
|
// throw new \LengthException('Argument value ' . $value . ' for color ' . $name
|
||||||
|
// . ' is not in the range of 0 to 255', 1);
|
||||||
|
// }
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get color
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public function __get(string $name): float
|
||||||
|
{
|
||||||
|
if (!property_exists($this, $name)) {
|
||||||
|
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||||
|
}
|
||||||
|
return $this->$name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the colorspace
|
||||||
|
*
|
||||||
|
* @param string $colorspace
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setColorspace(string $colorspace): self
|
||||||
|
{
|
||||||
|
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||||
|
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||||
|
}
|
||||||
|
$this->colorspace = $colorspace;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the whitepoint flag
|
||||||
|
*
|
||||||
|
* @param string $whitepoint
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
private function setWhitepoint(string $whitepoint): self
|
||||||
|
{
|
||||||
|
if (empty($whitepoint)) {
|
||||||
|
$this->whitepoint = '';
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
if (!in_array($whitepoint, $this::ILLUMINANT)) {
|
||||||
|
throw new \InvalidArgumentException('Not allowed whitepoint', 0);
|
||||||
|
}
|
||||||
|
$this->whitepoint = $whitepoint;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the color as array
|
||||||
|
* where 0: X, 1: Y, 2: Z
|
||||||
|
*
|
||||||
|
* @return array{0:float,1:float,2:float}
|
||||||
|
*/
|
||||||
|
public function returnAsArray(): array
|
||||||
|
{
|
||||||
|
return [$this->X, $this->Y, $this->Z];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set color as array
|
||||||
|
* where 0: X, 1: Y, 2: Z
|
||||||
|
*
|
||||||
|
* @param array{0:float,1:float,2:float} $colors
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function setFromArray(array $colors): self
|
||||||
|
{
|
||||||
|
$this->__set('X', $colors[0]);
|
||||||
|
$this->__set('Y', $colors[1]);
|
||||||
|
$this->__set('Z', $colors[2]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
54
www/lib/CoreLibs/Convert/Color/Stringify.php
Normal file
54
www/lib/CoreLibs/Convert/Color/Stringify.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AUTHOR: Clemens Schwaighofer
|
||||||
|
* CREATED: 2024/11/11
|
||||||
|
* DESCRIPTION:
|
||||||
|
* Convert color coordinate to CSS string
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert\Color;
|
||||||
|
|
||||||
|
use CoreLibs\Convert\Color\Coordinates\RGB;
|
||||||
|
use CoreLibs\Convert\Color\Coordinates\HSL;
|
||||||
|
use CoreLibs\Convert\Color\Coordinates\HWB;
|
||||||
|
use CoreLibs\Convert\Color\Coordinates\Lab;
|
||||||
|
use CoreLibs\Convert\Color\Coordinates\LCH;
|
||||||
|
|
||||||
|
class Stringify
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Build the opactiy sub string part and return it
|
||||||
|
*
|
||||||
|
* @param null|float|string|null $opacity
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function setOpacity(null|float|string $opacity = null): string
|
||||||
|
{
|
||||||
|
// set opacity, either a string or float
|
||||||
|
if (is_string($opacity)) {
|
||||||
|
$opacity = ' / ' . $opacity;
|
||||||
|
} elseif ($opacity !== null) {
|
||||||
|
$opacity = ' / ' . $opacity;
|
||||||
|
} else {
|
||||||
|
$opacity = '';
|
||||||
|
}
|
||||||
|
return $opacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return the CSS string including optional opacity
|
||||||
|
*
|
||||||
|
* @param RGB|Lab|LCH|HSL|HWB $data
|
||||||
|
* @param null|float|string $opacity
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function toCssString(RGB|Lab|LCH|HSL|HWB $data, null|float|string $opacity): string
|
||||||
|
{
|
||||||
|
return $data->toCssString($opacity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
@@ -17,6 +17,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace CoreLibs\Convert;
|
namespace CoreLibs\Convert;
|
||||||
|
|
||||||
|
use CoreLibs\Convert\Color\Color;
|
||||||
|
use CoreLibs\Convert\Color\Coordinates;
|
||||||
|
|
||||||
class Colors
|
class Colors
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -37,20 +40,7 @@ class Colors
|
|||||||
int $blue,
|
int $blue,
|
||||||
bool $hex_prefix = true
|
bool $hex_prefix = true
|
||||||
): string {
|
): string {
|
||||||
$hex_color = '';
|
return Coordinates\RGB::__constructFromArray([$red, $green, $blue])->returnAsHex($hex_prefix);
|
||||||
if ($hex_prefix === true) {
|
|
||||||
$hex_color = '#';
|
|
||||||
}
|
|
||||||
foreach (['red', 'green', 'blue'] as $color) {
|
|
||||||
// if not valid, abort
|
|
||||||
if ($$color < 0 || $$color > 255) {
|
|
||||||
throw new \LengthException('Argument value ' . $$color . ' for color ' . $color
|
|
||||||
. ' is not in the range of 0 to 255', 1);
|
|
||||||
}
|
|
||||||
// pad left with 0
|
|
||||||
$hex_color .= str_pad(dechex($$color), 2, '0', STR_PAD_LEFT);
|
|
||||||
}
|
|
||||||
return $hex_color;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,26 +59,21 @@ class Colors
|
|||||||
bool $return_as_string = false,
|
bool $return_as_string = false,
|
||||||
string $seperator = ','
|
string $seperator = ','
|
||||||
): string|array {
|
): string|array {
|
||||||
$hex_string = preg_replace("/[^0-9A-Fa-f]/", '', $hex_string); // Gets a proper hex string
|
|
||||||
if (!is_string($hex_string)) {
|
|
||||||
throw new \InvalidArgumentException('hex_string argument cannot be empty', 1);
|
|
||||||
}
|
|
||||||
$rgbArray = [];
|
$rgbArray = [];
|
||||||
if (strlen($hex_string) == 6) {
|
// rewrite to previous r/g/b key output
|
||||||
// If a proper hex code, convert using bitwise operation.
|
foreach (Coordinates\RGB::__constructFromHexString($hex_string)->returnAsArray() as $p => $el) {
|
||||||
// No overhead... faster
|
switch ($p) {
|
||||||
$colorVal = hexdec($hex_string);
|
case 0:
|
||||||
$rgbArray['r'] = 0xFF & ($colorVal >> 0x10);
|
$k = 'r';
|
||||||
$rgbArray['g'] = 0xFF & ($colorVal >> 0x8);
|
break;
|
||||||
$rgbArray['b'] = 0xFF & $colorVal;
|
case 1:
|
||||||
} elseif (strlen($hex_string) == 3) {
|
$k = 'g';
|
||||||
// If shorthand notation, need some string manipulations
|
break;
|
||||||
$rgbArray['r'] = hexdec(str_repeat(substr($hex_string, 0, 1), 2));
|
case 2:
|
||||||
$rgbArray['g'] = hexdec(str_repeat(substr($hex_string, 1, 1), 2));
|
$k = 'b';
|
||||||
$rgbArray['b'] = hexdec(str_repeat(substr($hex_string, 2, 1), 2));
|
break;
|
||||||
} else {
|
}
|
||||||
// Invalid hex color code
|
$rgbArray[$k] = (int)round($el);
|
||||||
throw new \UnexpectedValueException('Invalid hex_string: ' . $hex_string, 2);
|
|
||||||
}
|
}
|
||||||
// returns the rgb string or the associative array
|
// returns the rgb string or the associative array
|
||||||
return $return_as_string ? implode($seperator, $rgbArray) : $rgbArray;
|
return $return_as_string ? implode($seperator, $rgbArray) : $rgbArray;
|
||||||
@@ -108,39 +93,12 @@ class Colors
|
|||||||
*/
|
*/
|
||||||
public static function rgb2hsb(int $red, int $green, int $blue): array
|
public static function rgb2hsb(int $red, int $green, int $blue): array
|
||||||
{
|
{
|
||||||
// check that rgb is from 0 to 255
|
return array_map(
|
||||||
foreach (['red', 'green', 'blue'] as $color) {
|
fn ($v) => (int)round($v),
|
||||||
if ($$color < 0 || $$color > 255) {
|
Color::rgbToHsb(
|
||||||
throw new \LengthException('Argument value ' . $$color . ' for color ' . $color
|
Coordinates\RGB::__constructFromArray([$red, $green, $blue])
|
||||||
. ' is not in the range of 0 to 255', 1);
|
)->returnAsArray()
|
||||||
}
|
);
|
||||||
$$color = $$color / 255;
|
|
||||||
}
|
|
||||||
|
|
||||||
$MAX = max($red, $green, $blue);
|
|
||||||
$MIN = min($red, $green, $blue);
|
|
||||||
$HUE = 0;
|
|
||||||
|
|
||||||
if ($MAX == $MIN) {
|
|
||||||
return [0, 0, round($MAX * 100)];
|
|
||||||
}
|
|
||||||
if ($red == $MAX) {
|
|
||||||
$HUE = ($green - $blue) / ($MAX - $MIN);
|
|
||||||
} elseif ($green == $MAX) {
|
|
||||||
$HUE = 2 + (($blue - $red) / ($MAX - $MIN));
|
|
||||||
} elseif ($blue == $MAX) {
|
|
||||||
$HUE = 4 + (($red - $green) / ($MAX - $MIN));
|
|
||||||
}
|
|
||||||
$HUE *= 60;
|
|
||||||
if ($HUE < 0) {
|
|
||||||
$HUE += 360;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
(int)round($HUE),
|
|
||||||
(int)round((($MAX - $MIN) / $MAX) * 100),
|
|
||||||
(int)round($MAX * 100)
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -156,77 +114,12 @@ class Colors
|
|||||||
*/
|
*/
|
||||||
public static function hsb2rgb(float $H, float $S, float $V): array
|
public static function hsb2rgb(float $H, float $S, float $V): array
|
||||||
{
|
{
|
||||||
// check that H is 0 to 359, 360 = 0
|
return array_map(
|
||||||
// and S and V are 0 to 1
|
fn ($v) => (int)round($v),
|
||||||
if ($H == 360) {
|
Color::hsbToRgb(
|
||||||
$H = 0;
|
Coordinates\HSB::__constructFromArray([$H, $S, $V])
|
||||||
}
|
)->returnAsArray()
|
||||||
if ($H < 0 || $H > 359) {
|
);
|
||||||
throw new \LengthException('Argument value ' . $H . ' for hue is not in the range of 0 to 359', 1);
|
|
||||||
}
|
|
||||||
if ($S < 0 || $S > 100) {
|
|
||||||
throw new \LengthException('Argument value ' . $S . ' for saturation is not in the range of 0 to 100', 2);
|
|
||||||
}
|
|
||||||
if ($V < 0 || $V > 100) {
|
|
||||||
throw new \LengthException('Argument value ' . $V . ' for brightness is not in the range of 0 to 100', 3);
|
|
||||||
}
|
|
||||||
// convert to internal 0-1 format
|
|
||||||
$S /= 100;
|
|
||||||
$V /= 100;
|
|
||||||
|
|
||||||
if ($S == 0) {
|
|
||||||
$V = (int)round($V * 255);
|
|
||||||
return [$V, $V, $V];
|
|
||||||
}
|
|
||||||
|
|
||||||
$Hi = floor($H / 60);
|
|
||||||
$f = ($H / 60) - $Hi;
|
|
||||||
$p = $V * (1 - $S);
|
|
||||||
$q = $V * (1 - ($S * $f));
|
|
||||||
$t = $V * (1 - ($S * (1 - $f)));
|
|
||||||
|
|
||||||
switch ($Hi) {
|
|
||||||
case 0:
|
|
||||||
$red = $V;
|
|
||||||
$green = $t;
|
|
||||||
$blue = $p;
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
$red = $q;
|
|
||||||
$green = $V;
|
|
||||||
$blue = $p;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
$red = $p;
|
|
||||||
$green = $V;
|
|
||||||
$blue = $t;
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
$red = $p;
|
|
||||||
$green = $q;
|
|
||||||
$blue = $V;
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
$red = $t;
|
|
||||||
$green = $p;
|
|
||||||
$blue = $V;
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
$red = $V;
|
|
||||||
$green = $p;
|
|
||||||
$blue = $q;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
$red = 0;
|
|
||||||
$green = 0;
|
|
||||||
$blue = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
(int)round($red * 255),
|
|
||||||
(int)round($green * 255),
|
|
||||||
(int)round($blue * 255)
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -242,47 +135,12 @@ class Colors
|
|||||||
*/
|
*/
|
||||||
public static function rgb2hsl(int $red, int $green, int $blue): array
|
public static function rgb2hsl(int $red, int $green, int $blue): array
|
||||||
{
|
{
|
||||||
// check that rgb is from 0 to 255
|
return array_map(
|
||||||
foreach (['red', 'green', 'blue'] as $color) {
|
fn ($v) => round($v, 1),
|
||||||
if ($$color < 0 || $$color > 255) {
|
Color::rgbToHsl(
|
||||||
throw new \LengthException('Argument value ' . $$color . ' for color ' . $color
|
Coordinates\RGB::__constructFromArray([$red, $green, $blue])
|
||||||
. ' is not in the range of 0 to 255', 1);
|
)->returnAsArray()
|
||||||
}
|
);
|
||||||
$$color = $$color / 255;
|
|
||||||
}
|
|
||||||
|
|
||||||
$min = min($red, $green, $blue);
|
|
||||||
$max = max($red, $green, $blue);
|
|
||||||
$chroma = $max - $min;
|
|
||||||
$sat = 0;
|
|
||||||
$hue = 0;
|
|
||||||
// luminance
|
|
||||||
$lum = ($max + $min) / 2;
|
|
||||||
|
|
||||||
// achromatic
|
|
||||||
if ($chroma == 0) {
|
|
||||||
// H, S, L
|
|
||||||
return [0.0, 0.0, round($lum * 100, 1)];
|
|
||||||
} else {
|
|
||||||
$sat = $chroma / (1 - abs(2 * $lum - 1));
|
|
||||||
if ($max == $red) {
|
|
||||||
$hue = fmod((($green - $blue) / $chroma), 6);
|
|
||||||
if ($hue < 0) {
|
|
||||||
$hue = (6 - fmod(abs($hue), 6));
|
|
||||||
}
|
|
||||||
} elseif ($max == $green) {
|
|
||||||
$hue = ($blue - $red) / $chroma + 2;
|
|
||||||
} elseif ($max == $blue) {
|
|
||||||
$hue = ($red - $green) / $chroma + 4;
|
|
||||||
}
|
|
||||||
$hue = $hue * 60;
|
|
||||||
// $sat = 1 - abs(2 * $lum - 1);
|
|
||||||
return [
|
|
||||||
round($hue, 1),
|
|
||||||
round($sat * 100, 1),
|
|
||||||
round($lum * 100, 1)
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -297,54 +155,12 @@ class Colors
|
|||||||
*/
|
*/
|
||||||
public static function hsl2rgb(float $hue, float $sat, float $lum): array
|
public static function hsl2rgb(float $hue, float $sat, float $lum): array
|
||||||
{
|
{
|
||||||
if ($hue == 360) {
|
return array_map(
|
||||||
$hue = 0;
|
fn ($v) => round($v),
|
||||||
}
|
Color::hslToRgb(
|
||||||
if ($hue < 0 || $hue > 359) {
|
Coordinates\HSL::__constructFromArray([$hue, $sat, $lum])
|
||||||
throw new \LengthException('Argument value ' . $hue . ' for hue is not in the range of 0 to 359', 1);
|
)->returnAsArray()
|
||||||
}
|
);
|
||||||
if ($sat < 0 || $sat > 100) {
|
|
||||||
throw new \LengthException('Argument value ' . $sat . ' for saturation is not in the range of 0 to 100', 2);
|
|
||||||
}
|
|
||||||
if ($lum < 0 || $lum > 100) {
|
|
||||||
throw new \LengthException('Argument value ' . $lum . ' for luminance is not in the range of 0 to 100', 3);
|
|
||||||
}
|
|
||||||
// calc to internal convert value for hue
|
|
||||||
$hue = (1 / 360) * $hue;
|
|
||||||
// convert to internal 0-1 format
|
|
||||||
$sat /= 100;
|
|
||||||
$lum /= 100;
|
|
||||||
// if saturation is 0
|
|
||||||
if ($sat == 0) {
|
|
||||||
$lum = (int)round($lum * 255);
|
|
||||||
return [$lum, $lum, $lum];
|
|
||||||
} else {
|
|
||||||
$m2 = $lum < 0.5 ? $lum * ($sat + 1) : ($lum + $sat) - ($lum * $sat);
|
|
||||||
$m1 = $lum * 2 - $m2;
|
|
||||||
$hueue = function ($base) use ($m1, $m2) {
|
|
||||||
// base = hue, hue > 360 (1) - 360 (1), else < 0 + 360 (1)
|
|
||||||
$base = $base < 0 ? $base + 1 : ($base > 1 ? $base - 1 : $base);
|
|
||||||
// 6: 60, 2: 180, 3: 240
|
|
||||||
// 2/3 = 240
|
|
||||||
// 1/3 = 120 (all from 360)
|
|
||||||
if ($base * 6 < 1) {
|
|
||||||
return $m1 + ($m2 - $m1) * $base * 6;
|
|
||||||
}
|
|
||||||
if ($base * 2 < 1) {
|
|
||||||
return $m2;
|
|
||||||
}
|
|
||||||
if ($base * 3 < 2) {
|
|
||||||
return $m1 + ($m2 - $m1) * ((2 / 3) - $base) * 6;
|
|
||||||
}
|
|
||||||
return $m1;
|
|
||||||
};
|
|
||||||
|
|
||||||
return [
|
|
||||||
(int)round(255 * $hueue($hue + (1 / 3))),
|
|
||||||
(int)round(255 * $hueue($hue)),
|
|
||||||
(int)round(255 * $hueue($hue - (1 / 3)))
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,6 +56,95 @@ class Math
|
|||||||
return (float)$number;
|
return (float)$number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* calc cube root
|
||||||
|
*
|
||||||
|
* @param float $number Number to cubic root
|
||||||
|
* @return float Calculated value
|
||||||
|
*/
|
||||||
|
public static function cbrt(float|int $number): float
|
||||||
|
{
|
||||||
|
return pow((float)$number, 1.0 / 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function is directly inspired by the multiplyMatrices() function in color.js
|
||||||
|
* form Lea Verou and Chris Lilley.
|
||||||
|
* (see https://github.com/LeaVerou/color.js/blob/main/src/multiply-matrices.js)
|
||||||
|
* From:
|
||||||
|
* https://github.com/matthieumastadenis/couleur/blob/3842cf51c9517e77afaa0a36ec78643a0c258e0b/src/utils/utils.php#L507
|
||||||
|
*
|
||||||
|
* It returns an array which is the product of the two number matrices passed as parameters.
|
||||||
|
*
|
||||||
|
* @param array<array<int|float>> $a m x n matrice
|
||||||
|
* @param array<array<int|float>> $b n x p matrice
|
||||||
|
*
|
||||||
|
* @return array<array<int|float>> m x p product
|
||||||
|
*/
|
||||||
|
public static function multiplyMatrices(array $a, array $b): array
|
||||||
|
{
|
||||||
|
$m = count($a);
|
||||||
|
|
||||||
|
if (!is_array($a[0] ?? null)) {
|
||||||
|
// $a is vector, convert to [[a, b, c, ...]]
|
||||||
|
$a = [ $a ];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_array($b[0])) {
|
||||||
|
// $b is vector, convert to [[a], [b], [c], ...]]
|
||||||
|
$b = array_map(
|
||||||
|
callback: fn ($v) => [ $v ],
|
||||||
|
array: $b,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$p = count($b[0]);
|
||||||
|
|
||||||
|
// transpose $b:
|
||||||
|
$bCols = array_map(
|
||||||
|
callback: fn ($k) => \array_map(
|
||||||
|
(fn ($i) => $i[$k]),
|
||||||
|
$b,
|
||||||
|
),
|
||||||
|
array: array_keys($b[0]),
|
||||||
|
);
|
||||||
|
|
||||||
|
$product = array_map(
|
||||||
|
callback: fn ($row) => array_map(
|
||||||
|
callback: fn ($col) => is_array($row) ?
|
||||||
|
array_reduce(
|
||||||
|
array: $row,
|
||||||
|
callback: fn ($a, $v, $i = null) => $a + $v * (
|
||||||
|
$col[$i ?? array_search($v, $row)] ?? 0
|
||||||
|
),
|
||||||
|
initial: 0,
|
||||||
|
) :
|
||||||
|
array_reduce(
|
||||||
|
array: $col,
|
||||||
|
callback: fn ($a, $v) => $a + $v * $row,
|
||||||
|
initial: 0,
|
||||||
|
),
|
||||||
|
array: $bCols,
|
||||||
|
),
|
||||||
|
array: $a,
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($m === 1) {
|
||||||
|
// Avoid [[a, b, c, ...]]:
|
||||||
|
$product = $product[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($p === 1) {
|
||||||
|
// Avoid [[a], [b], [c], ...]]:
|
||||||
|
return array_map(
|
||||||
|
callback: fn ($v) => $v[0],
|
||||||
|
array: $product,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $product;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
Reference in New Issue
Block a user