8000 minor #50317 [Console] Remove redundant method getSaturation() (ivanp… · symfony/symfony@df086fa · GitHub
[go: up one dir, main page]

Skip to content

Commit df086fa

Browse files
committed
minor #50317 [Console] Remove redundant method getSaturation() (ivanpepelko)
This PR was submitted for the 6.2 branch but it was merged into the 6.3 branch instead. Discussion ---------- [Console] Remove redundant method getSaturation() | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | none | License | MIT | Doc PR | none 1. in `getSaturation()`, `$diff` is compared against `int` zero. This will only yield `true` when RGB values contain min/max color value (`0` or `255`). It will be always `false` for floats. 2. in `degradeHexColorToAnsi4()` value returned from `getSaturation()` is `round`ed, which returns float, thus making comparison to `int` zero useless. 3. in the same method, bitwise operations (final `return` statement) will convert number to `int` anyway, so the explicit cast was removed In conclusion, `getSaturation` is redundant. Tests are expanded to show that even in min/max cases everything works fine. Commits ------- 38021b5 [Console] Remove redundant method getSaturation()
2 parents eb3e308 + 38021b5 commit df086fa

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

src/Symfony/Component/Console/Output/AnsiColorMode.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,7 @@ private function convertFromRGB(int $r, int $g, int $b): int
7878

7979
private function degradeHexColorToAnsi4(int $r, int $g, int $b): int
8080
{
81-
if (0 === round($this->getSaturation($r, $g, $b) / 50)) {
82-
return 0;
83-
}
84-
85-
return (int) ((round($b / 255) << 2) | (round($g / 255) << 1) | round($r / 255));
86-
}
87-
88-
private function getSaturation(int $r, int $g, int $b): int
89-
{
90-
$r = $r / 255;
91-
$g = $g / 255;
92-
$b = $b / 255;
93-
$v = max($r, $g, $b);
94-
95-
if (0 === $diff = $v - min($r, $g, $b)) {
96-
return 0;
97-
}
98-
99-
return (int) ((int) $diff * 100 / $v);
81+
return round($b / 255) << 2 | (round($g / 255) << 1) | round($r / 255);
10082
}
10183

10284
/**

src/Symfony/Component/Console/Tests/Output/AnsiColorModeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ public static function provideColorsConversion(): \Generator
6464
AnsiColorMode::Ansi8->name => 248,
6565
AnsiColorMode::Ansi4->name => 7,
6666
]];
67+
68+
yield ['#000000', [
69+
AnsiColorMode::Ansi8->name => 16,
70+
AnsiColorMode::Ansi4->name => 0,
71+
]];
72+
73+
yield ['#ffffff', [
74+
AnsiColorMode::Ansi8->name => 231,
75+
AnsiColorMode::Ansi4->name => 7,
76+
]];
6777
}
6878

6979
public function testColorsConversionWithoutSharp()

0 commit comments

Comments
 (0)
0