Skip to content

New Class ChartColor and Refactoring #2898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2022
Merged
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
30 changes: 21 additions & 9 deletions src/PhpSpreadsheet/Chart/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
*/
class Axis extends Properties
{
public function __construct()
{
parent::__construct();
$this->fillColor = new ChartColor();
}

/**
* Axis Number.
*
Expand Down Expand Up @@ -42,13 +48,9 @@ class Axis extends Properties
/**
* Fill Properties.
*
* @var mixed[]
* @var ChartColor
*/
private $fillProperties = [
'type' => self::EXCEL_COLOR_TYPE_ARGB,
'value' => null,
'alpha' => 0,
];
private $fillColor;

private const NUMERIC_FORMAT = [
Properties::FORMAT_CODE_NUMBER,
Expand Down Expand Up @@ -163,7 +165,7 @@ public function setAxisOrientation($orientation): void
*/
public function setFillParameters($color, $alpha = null, $AlphaType = self::EXCEL_COLOR_TYPE_ARGB): void
{
$this->fillProperties = $this->setColorProperties($color, $alpha, $AlphaType);
$this->fillColor->setColorProperties($color, $alpha, $AlphaType);
}

/**
Expand All @@ -175,19 +177,29 @@ public function setFillParameters($color, $alpha = null, $AlphaType = self::EXCE
*/
public function getFillProperty($property)
{
return (string) $this->fillProperties[$property];
return (string) $this->fillColor->getColorProperty($property);
}

public function getFillColorObject(): ChartColor
{
return $this->fillColor;
}

/**
* Get Line Color Property.
*
* @Deprecated 1.24.0
*
* @See Properties::getLineColorProperty()
* Use the getLineColor property in the Properties class instead
*
* @param string $propertyName
*
* @return null|int|string
*/
public function getLineProperty($propertyName)
{
return $this->lineProperties['color'][$propertyName];
return $this->getLineColorProperty($propertyName);
}

/** @var string */
Expand Down
133 changes: 133 additions & 0 deletions src/PhpSpreadsheet/Chart/ChartColor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace PhpOffice\PhpSpreadsheet\Chart;

class ChartColor
{
const EXCEL_COLOR_TYPE_STANDARD = 'prstClr';
const EXCEL_COLOR_TYPE_SCHEME = 'schemeClr';
const EXCEL_COLOR_TYPE_ARGB = 'srgbClr';
const EXCEL_COLOR_TYPES = [
self::EXCEL_COLOR_TYPE_ARGB,
self::EXCEL_COLOR_TYPE_SCHEME,
self::EXCEL_COLOR_TYPE_STANDARD,
];

/** @var string */
private $value = '';

/** @var string */
private $type = '';

/** @var ?int */
private $alpha;

public function getValue(): string
{
return $this->value;
}

public function setValue(string $value): self
{
$this->value = $value;

return $this;
}

public function getType(): string
{
return $this->type;
}

public function setType(string $type): self
{
$this->type = $type;

return $this;
}

public function getAlpha(): ?int
{
return $this->alpha;
}

public function setAlpha(?int $alpha): self
{
$this->alpha = $alpha;

return $this;
}

/**
* @param null|float|int|string $alpha
*/
public function setColorProperties(?string $color, $alpha, ?string $type): self
{
if ($color !== null) {
$this->setValue($color);
}
if ($type !== null) {
$this->setType($type);
}
if ($alpha === null) {
$this->setAlpha(null);
} elseif (is_numeric($alpha)) {
$this->setAlpha((int) $alpha);
}

return $this;
}

public function setColorPropertiesArray(array $color): self
{
if (array_key_exists('value', $color) && is_string($color['value'])) {
$this->setValue($color['value']);
}
if (array_key_exists('type', $color) && is_string($color['type'])) {
$this->setType($color['type']);
}
if (array_key_exists('alpha', $color)) {
if ($color['alpha'] === null) {
$this->setAlpha(null);
} elseif (is_numeric($color['alpha'])) {
$this->setAlpha((int) $color['alpha']);
}
}

return $this;
}

/**
* Get Color Property.
*
* @param string $propertyName
*
* @return null|int|string
*/
public function getColorProperty($propertyName)
{
$retVal = null;
if ($propertyName === 'value') {
$retVal = $this->value;
} elseif ($propertyName === 'type') {
$retVal = $this->type;
} elseif ($propertyName === 'alpha') {
$retVal = $this->alpha;
}

return $retVal;
}

public static function alphaToXml(int $alpha): string
{
return (string) (100 - $alpha) . '000';
}

/**
* @param float|int|string $alpha
*/
public static function alphaFromXml($alpha): int
{
return 100 - ((int) $alpha / 1000);
}
}
Loading