Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Please also have a look at our

### Documentation

## 9.2.0:

### Added

- Add use modern CSS color syntax option to `OutputFormat` (#1442)

## 9.1.0: Add support for PHP 8.5

### Added
Expand Down
25 changes: 25 additions & 0 deletions src/OutputFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ final class OutputFormat
*/
private $usesRgbHashNotation = true;

/**
* Output RGB colors in CSS 4 notation if possible
*
* @var bool
*/
private $usesModernColorSyntax = false;

/**
* Declaration format
*
Expand Down Expand Up @@ -220,6 +227,24 @@ public function setRGBHashNotation(bool $usesRgbHashNotation): self
return $this;
}

/**
* @internal
*/
public function usesModernColorSyntax(): bool
{
return $this->usesModernColorSyntax;
}

/**
* @return $this fluent interface
*/
public function setUseModernColorSyntax(bool $usesModernColorSyntax): self
{
$this->usesModernColorSyntax = $usesModernColorSyntax;

return $this;
}

/**
* @internal
*/
Expand Down
14 changes: 10 additions & 4 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function render(OutputFormat $outputFormat): string
return $this->renderAsHex();
}

if ($this->shouldRenderInModernSyntax()) {
if ($this->shouldRenderInModernSyntax($outputFormat)) {
return $this->renderInModernSyntax($outputFormat);
}

Expand Down Expand Up @@ -309,9 +309,9 @@ private function renderAsHex(): string
* The same in the CSS Color Module Level 4 W3C Candidate Recommendation Draft
* } (as of 13 February 2024, at time of writing).
*/
private function shouldRenderInModernSyntax(): bool
private function shouldRenderInModernSyntax(OutputFormat $outputFormat): bool
{
if ($this->hasNoneAsComponentValue()) {
if ($this->hasNoneAsComponentValue() || $outputFormat->usesModernColorSyntax()) {
return true;
}

Expand Down Expand Up @@ -385,6 +385,12 @@ private function renderInModernSyntax(OutputFormat $outputFormat): string
$arguments = $formatter->implode($separator, [$arguments, $alpha]);
}

return $this->getName() . '(' . $arguments . ')';
$name = $this->getName();

if ($outputFormat->usesModernColorSyntax()) {
$name = str_replace('a', '', $name);
}

return $name . '(' . $arguments . ')';
}
}
Loading
Loading