diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3c20bc3..5f937c8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,14 @@
# (MODX)EvolutionCMS.snippets.ddColorTools changelog
+## Version 3.2 (2023-03-10)
+* \+ Parameters → `inputColor`: Supports values with alpha-channel.
+ * \+ `offset_a`: The new parameter. Allows you to modify alpha-channel.
+ * \+ `result_tpl` → Placeholders → `[+ddA+]`: The new placeholder.
+* \* `\ddTools::getTpl` is used instead of `$modx->getTpl`.
+* \* Attention! (MODX)EvolutionCMS.libraries.ddTools >= 0.60 is required.
+
+
## Version 3.1 (2023-03-10)
* \* Attention! (MODX)EvolutionCMS.libraries.ddTools >= 0.57 is required.
* \* Attention! (MODX)EvolutionCMS.snippets.ddGetDocumentField >= 2.11.1 is required.
@@ -53,5 +61,5 @@
* \+ Initial commit.
-
+
\ No newline at end of file
diff --git a/CHANGELOG_ru.md b/CHANGELOG_ru.md
index 92baac1..7c1bfa7 100644
--- a/CHANGELOG_ru.md
+++ b/CHANGELOG_ru.md
@@ -1,6 +1,14 @@
# (MODX)EvolutionCMS.snippets.ddColorTools changelog
+## Версия 3.2 (2023-03-10)
+* \+ Parameters → `inputColor`: Supports values with alpha-channel.
+ * \+ `offset_a`: The new parameter. Allows you to modify alpha-channel.
+ * \+ `result_tpl` → Placeholders → `[+ddA+]`: The new placeholder.
+* \* `\ddTools::getTpl` is used instead of `$modx->getTpl`.
+* \* Attention! (MODX)EvolutionCMS.libraries.ddTools >= 0.60 is required.
+
+
## Версия 3.1 (2023-03-10)
* \* Внимание! Требуется (MODX)EvolutionCMS.libraries.ddTools >= 0.57.
* \* Внимание! Требуется (MODX)EvolutionCMS.snippets.ddGetDocumentField >= 2.11.1.
@@ -53,5 +61,5 @@
* \+ Начальный кэммит.
-
+
\ No newline at end of file
diff --git a/README.md b/README.md
index 189fbcd..ff6036d 100644
--- a/README.md
+++ b/README.md
@@ -6,8 +6,8 @@ Converts the color to match the offset in tone, brightness, or saturation.
## Requires
* PHP >= 5.6
-* [(MODX)EvolutionCMS.libraries.ddTools](https://code.divandesign.biz/modx/ddtools) >= 0.57
-* [(MODX)EvolutionCMS.snippets.ddGetDocumentField](https://code.divandesign.biz/modx/ddgetdocumentfield) >= 2.11.1
+* [(MODX)EvolutionCMS.libraries.ddTools](https://code.divandesign.ru/modx/ddtools) >= 0.60
+* [(MODX)EvolutionCMS.snippets.ddGetDocumentField](https://code.divandesign.ru/modx/ddgetdocumentfield) >= 2.11.1
## Installation
@@ -19,7 +19,7 @@ Converts the color to match the offset in tone, brightness, or saturation.
#### 1. Elements → Snippets: Create a new snippet with the following data
1. Snippet name: `ddColorTools`.
-2. Description: `3.1 Converts the color to match the offset in tone, brightness, or saturation.`.
+2. Description: `3.2 Converts the color to match the offset in tone, brightness, or saturation.`.
3. Category: `Core`.
4. Parse DocBlock: `no`.
5. Snippet code (php): Insert content of the `ddColorTools_snippet.php` file from the archive.
@@ -59,15 +59,19 @@ require_once(
### Input color
* `inputColor`
- * Desctription: Input color as HEX, HSL or HSB/HSV.
+ * Desctription: Input color as HEX, HSL or HSB/HSV, with or without alpha-channel.
Case-insensitive.
Valid format examples:
* `ffffff`
* `#FFFFFF`
+ * `#FFFFFFFF`
* `hsl(0, 0%, 100%)`
+ * `hsla(0, 0%, 100%)`
* `HSL(0, 0, 100)`
* `hsb(0, 0%, 100%)`
+ * `hsba(0, 0%, 100%)`
* `hsv(0, 0%, 100%)`
+ * `hsva(0, 0%, 100%)`
* `hsb(0, 0, 100)`
* `hsv(0, 0, 100)`
* Valid values: `string`
@@ -124,6 +128,16 @@ All parameters can contain the following special operators:
* Desctription: Offset of the lightness in persents (`[-100; +100]`).
* Valid values: `string`
* **Required**
+
+* `offset_a`
+ * Desctription: Operations of the alpha-channel offset separated by commas.
+ * Valid values: `stringCommaSeparated`
+ * Default value: `'+0'`
+
+* `offset_a[i]`
+ * Desctription: Offset of the alpha-channel in persents (`[-100; +100]`).
+ * Valid values: `string`
+ * **Required**
### Output
@@ -144,6 +158,7 @@ All parameters can contain the following special operators:
* `[+ddH+]` — hue
* `[+ddS+]` — saturation
* `[+ddL+]` — lightness
+ * `[+ddA+]` — alpha-channel
* `[+ddIsDark+]` — is color dark (`0` || `1`)?
* Valid values:
* `stringChunkName`
@@ -217,10 +232,10 @@ require_once(
## Links
-* [Home page](https://code.divandesign.biz/modx/ddcolortools)
+* [Home page](https://code.divandesign.ru/modx/ddcolortools)
* [Telegram chat](https://t.me/dd_code)
* [Packagist](https://packagist.org/packages/dd/evolutioncms-snippets-ddcolortools)
* [GitHub](https://github.com/DivanDesign/EvolutionCMS.snippets.ddColorTools)
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/README_ru.md b/README_ru.md
index b1fdef6..a4c59bd 100644
--- a/README_ru.md
+++ b/README_ru.md
@@ -6,8 +6,8 @@
## Использует
* PHP >= 5.6
-* [(MODX)EvolutionCMS.libraries.ddTools](https://code.divandesign.biz/modx/ddtools) >= 0.57
-* [(MODX)EvolutionCMS.snippets.ddGetDocumentField](https://code.divandesign.biz/modx/ddgetdocumentfield) >= 2.11.1
+* [(MODX)EvolutionCMS.libraries.ddTools](https://code.divandesign.ru/modx/ddtools) >= 0.60
+* [(MODX)EvolutionCMS.snippets.ddGetDocumentField](https://code.divandesign.ru/modx/ddgetdocumentfield) >= 2.11.1
## Установка
@@ -19,7 +19,7 @@
#### 1. Элементы → Сниппеты: Создайте новый сниппет со следующими параметрами
1. Название сниппета: `ddColorTools`.
-2. Описание: `3.1 Преобразует цвет в соответствии со смещением по тону, яркости или насыщенности.`.
+2. Описание: `3.2 Преобразует цвет в соответствии со смещением по тону, яркости или насыщенности.`.
3. Категория: `Core`.
4. Анализировать DocBlock: `no`.
5. Код сниппета (php): Вставьте содержимое файла `ddColorTools_snippet.php` из архива.
@@ -59,15 +59,19 @@ require_once(
### Исходный цвет
* `inputColor`
- * Описание: Исходный цвет в HEX, HSL или HSB/HSV.
+ * Описание: Исходный цвет в HEX, HSL или HSB/HSV, с альфа-каналом или без.
Значение регистронезависимо.
Примеры валидных значений:
* `ffffff`
* `#FFFFFF`
+ * `#FFFFFFFF`
* `hsl(0, 0%, 100%)`
+ * `hsla(0, 0%, 100%)`
* `HSL(0, 0, 100)`
* `hsb(0, 0%, 100%)`
+ * `hsba(0, 0%, 100%)`
* `hsv(0, 0%, 100%)`
+ * `hsva(0, 0%, 100%)`
* `hsb(0, 0, 100)`
* `hsv(0, 0, 100)`
* Допустимые значения: `string`
@@ -124,6 +128,16 @@ require_once(
* Описание: Смещение яркости в процентах (`[-100; +100]`).
* Допустимые значения: `string`
* **Обязателен**
+
+* `offset_a`
+ * Описание: Операции смещения альфа-канала через запятую.
+ * Допустимые значения: `stringCommaSeparated`
+ * Значение по умолчанию: `'+0'`
+
+* `offset_a[i]`
+ * Описание: Смещение альфа-канала в процентах (`[-100; +100]`).
+ * Допустимые значения: `string`
+ * **Обязателен**
### Вывод результата
@@ -144,6 +158,7 @@ require_once(
* `[+ddH+]` — цветовой тон
* `[+ddS+]` — насыщенность
* `[+ddL+]` — яркость
+ * `[+ddA+]` — альфа-канал
* `[+ddIsDark+]` — является ли цвет тёмным (`0` || `1`)
* Допустимые значения:
* `stringChunkName`
@@ -223,4 +238,4 @@ require_once(
* [GitHub](https://github.com/DivanDesign/EvolutionCMS.snippets.ddColorTools)
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/composer.json b/composer.json
index 862a69b..e125149 100644
--- a/composer.json
+++ b/composer.json
@@ -1,7 +1,7 @@
{
"name": "dd/evolutioncms-snippets-ddcolortools",
"type": "modxevo-snippet",
- "version": "3.1.0",
+ "version": "3.2.0",
"description": "Converts the color to match the offset in tone, brightness, or saturation.",
"keywords": [
"modx",
@@ -22,17 +22,11 @@
"rgb to hsl",
"hsl to rgb"
],
- "homepage": "https://code.divandesign.biz/modx/ddstartsnippet",
+ "homepage": "https://code.divandesign.ru/modx/ddstartsnippet",
"support": {
"chat": "https://t.me/dd_code"
},
"authors": [
- {
- "name": "DD Group",
- "email": "code@DivanDesign.biz",
- "homepage": "https://code.divandesign.biz",
- "role": "Owner"
- },
{
"name": "Ilyas Ronef",
"email": "ilyas@DivanDesign.ru",
@@ -47,7 +41,7 @@
],
"require": {
"php": ">=5.6.0",
- "dd/evolutioncms-libraries-ddtools": ">=0.57.0",
+ "dd/evolutioncms-libraries-ddtools": ">=0.60.0",
"dd/evolutioncms-snippets-ddgetdocumentfield": ">=2.11.1"
}
}
\ No newline at end of file
diff --git a/ddColorTools_snippet.php b/ddColorTools_snippet.php
index 3d89752..74bb40e 100644
--- a/ddColorTools_snippet.php
+++ b/ddColorTools_snippet.php
@@ -1,13 +1,13 @@
'+0',
'offset_s' => '+0',
'offset_l' => '+0',
+ 'offset_a' => '+0',
'result_outputFormat' => 'hsl',
'result_tpl' => '',
'result_tpl_placeholders' => [],
@@ -25,7 +26,7 @@ class Snippet extends \DDTools\Snippet {
/**
* prepareParams
- * @version 1.0 (2023-03-10)
+ * @version 1.1 (2024-01-19)
*
* @param $this->params {stdClass|arrayAssociative|stringJsonObject|stringHjsonObject|stringQueryFormatted}
*
@@ -65,6 +66,7 @@ protected function prepareParams($paramsRenameMe = []){
'offset_h',
'offset_s',
'offset_l',
+ 'offset_a',
] as
$paramName
){
@@ -74,12 +76,12 @@ protected function prepareParams($paramsRenameMe = []){
);
}
- $this->params->result_tpl = \ddTools::$modx->getTpl($this->params->result_tpl);
+ $this->params->result_tpl = \ddTools::getTpl($this->params->result_tpl);
}
/**
* run
- * @version 1.4 (2023-03-10)
+ * @version 1.5 (2024-01-19)
*
* @return {string}
*/
@@ -92,17 +94,33 @@ public function run(){
$hslRange = (object) [
'h' => $this->params->offset_h,
's' => $this->params->offset_s,
- 'l' => $this->params->offset_l
+ 'l' => $this->params->offset_l,
+ 'a' => $this->params->offset_l,
];
$hslMax = (object) [
'h' => 360,
's' => 100,
- 'l' => 100
+ 'l' => 100,
+ 'a' => 100,
];
$resultColorHsl = static::stringToHsl($this->params->inputColor);
+ $isAlphaUsed = \DDTools\ObjectTools::isPropExists([
+ 'object' => $resultColorHsl,
+ 'propName' => 'a'
+ ]);
+
+ if (
+ !$isAlphaUsed
+ && $this->params->offset_a != '+0'
+ ){
+ $isAlphaUsed = true;
+
+ $resultColorHsl->a = 100;
+ }
+
foreach(
$resultColorHsl as
$key =>
@@ -199,17 +217,32 @@ public function run(){
if($resultColorHsl->l > $hslMax->l){
$resultColorHsl->l = $hslMax->l;
}
+ if(
+ $isAlphaUsed
+ && $resultColorHsl->a > $hslMax->a
+ ){
+ $resultColorHsl->a = $hslMax->a;
+ }
switch($this->params->result_outputFormat){
case 'hsl':
$result =
- 'hsl(' .
- $resultColorHsl->h .
- ',' .
- $resultColorHsl->s .
- '%,' .
- $resultColorHsl->l .
- '%)'
+ 'hsl'
+ . (
+ $isAlphaUsed
+ ? 'a'
+ : ''
+ )
+ . '('
+ . $resultColorHsl->h . ','
+ . $resultColorHsl->s . '%,'
+ . $resultColorHsl->l . '%'
+ . (
+ $isAlphaUsed
+ ? $resultColorHsl->a . '%'
+ : ''
+ )
+ . ')'
;
break;
@@ -225,13 +258,22 @@ public function run(){
);
$result =
- 'rgb(' .
- $result->r .
- ',' .
- $result->g .
- ',' .
- $result->b .
- ')'
+ 'rgb('
+ . (
+ $isAlphaUsed
+ ? 'a'
+ : ''
+ )
+ . '('
+ . $result->r . ','
+ . $result->g . ','
+ . $result->b
+ . (
+ $isAlphaUsed
+ ? $result->a
+ : ''
+ )
+ . ')'
;
break;
}
@@ -242,6 +284,7 @@ public function run(){
'ddH' => $resultColorHsl->h,
'ddS' => $resultColorHsl->s,
'ddL' => $resultColorHsl->l,
+ 'ddA' => $resultColorHsl->a,
'ddIsDark' => intval(
static::isRgbDark(
static::hsbToRgb(
@@ -273,7 +316,7 @@ public function run(){
/**
* stringToHsl
- * @version 1.0 (2023-03-10)
+ * @version 1.1 (2024-01-19)
*
* @param $paramString {string} — Color string in the HEX, HSL or HSB/HSV formats. @required
*
@@ -281,6 +324,7 @@ public function run(){
* @return $result->h {integer}
* @return $result->s {integer}
* @return $result->l {integer}
+ * @return $result->a {integer} — Can be absent.
*/
private static function stringToHsl($paramString): \stdClass {
//If input color set as HSL || HSB/HSV
@@ -301,8 +345,11 @@ private static function stringToHsl($paramString): \stdClass {
//Remove unwanted chars
$paramString = str_replace(
[
+ 'hsla',
'hsl',
+ 'hsba',
'hsb',
+ 'hsva',
'hsv',
'%',
'(',
@@ -336,6 +383,16 @@ private static function stringToHsl($paramString): \stdClass {
'b' => $paramString[2]
]);
}
+
+ //Alpha-channel
+ if (
+ \DDTools\ObjectTools::isPropExists([
+ 'object' => $paramString,
+ 'propName' => 3
+ ])
+ ){
+ $resultHsl->a = $paramString[3];
+ }
//AS RGB
}else{
//Удалим из цвета символ '#'
@@ -349,35 +406,71 @@ private static function stringToHsl($paramString): \stdClass {
$resultHsl = static::hexToHsl($paramString);
}
+ //Alpha-channel
+ if (
+ \DDTools\ObjectTools::isPropExists([
+ 'object' => $resultHsl,
+ 'propName' => 'a'
+ ])
+ ){
+ if ($resultHsl->a < 1){
+ $resultHsl->a = $resultHsl->a * 100;
+ }
+
+ if (
+ !is_numeric($resultHsl->a) ||
+ $resultHsl->a == 100
+ ){
+ unset($resultHsl->a);
+ }
+ }
+
return $resultHsl;
}
/**
* hexToHsl
- * @version 3.0.1 (2023-03-10$this->params->inputColor$hex {string} — Color in HEX format without first '#'. @required
+ * @version 3.1 (2024-01-19)
+ *
+ * @param $hexString {string} — Color in HEX format without first '#'. @required
*
* @return $result {stdClass}
* @return $result->h {integer}
* @return $result->s {integer}
* @return $result->l {integer}
+ * @return $result->a {integer} — Can be absent.
*/
- private static function hexToHsl($hex): \stdClass {
+ private static function hexToHsl($hexString): \stdClass {
+ $resultHsl = new \stdClass();
+
//Получаем цвета в 10чной системе
$red = hexdec(substr(
- $hex,
+ $hexString,
0,
2
));
$green = hexdec(substr(
- $hex,
+ $hexString,
2,
2
));
$blue = hexdec(substr(
- $hex,
+ $hexString,
4,
2
));
+ $alpha = substr(
+ $hexString,
+ 6,
+ 2
+ );
+ if (!empty($alpha)){
+ $resultHsl->a =
+ hexdec($alpha)
+ / 255
+ * 100
+ ;
+ }
//Находим максимальное и минимальное значения
$max = max(
@@ -391,8 +484,6 @@ private static function hexToHsl($hex): \stdClass {
$blue
);
- $resultHsl = new \stdClass();
-
//Вычисляем яркость (от 0 до 100)
$resultHsl->l = round(
($max + $min) / 2 * 100 / 255
@@ -456,17 +547,19 @@ private static function hexToHsl($hex): \stdClass {
/**
* hsbToRgb
- * @version 2.0.1 (2023-03-10)
+ * @version 2.1 (2024-01-19)
*
* @param $paramHsb {stdClass|arrayAssociative} — Color in HSB format. @required
* @param $paramHsb->h {integer} — Hue. @required
* @param $paramHsb->s {integer} — Saturation. @required
* @param $paramHsb->b {integer} — Brightness. @required
+ * @param $paramHsb->a {integer} — Alpha-channel. Default —.
*
* @return $result {stdClass}
* @return $result->r {integer}
* @return $result->g {integer}
* @return $result->b {integer}
+ * @return $result->a {integer} — Can be absent.
*/
private static function hsbToRgb($paramHsb): \stdClass {
$paramHsb = (object) $paramHsb;
@@ -538,6 +631,20 @@ private static function hsbToRgb($paramHsb): \stdClass {
}
}
+ if (
+ \DDTools\ObjectTools::isPropExists([
+ 'object' => $paramHsb,
+ 'propName' => 'a'
+ ])
+ && $paramHsb->a < 100
+ ){
+ $resultRgb->a = $paramHsb->a;
+
+ if ($resultRgb->a < 1){
+ $resultRgb->a = $resultRgb->a * 100;
+ }
+ }
+
//Переводим из системы счисления от 0 до 100 в от 0 до 255
foreach (
$resultRgb as
@@ -560,6 +667,7 @@ private static function hsbToRgb($paramHsb): \stdClass {
* @param $paramRgb->r {integer} — Hue. @required
* @param $paramRgb->g {integer} — Saturation. @required
* @param $paramRgb->b {integer} — Brightness. @required
+ * @param $paramRgb->a {integer} — Alpha-channel. Default —.
*
* @return $result {string}
*/
@@ -594,6 +702,7 @@ private static function rgbToHex($paramRgb): string {
* @param $paramHsb->h {integer} — Hue. @required
* @param $paramHsb->s {integer} — Saturation. @required
* @param $paramHsb->b {integer} — Brightness. @required
+ * @param $paramHsb->a {integer} — Alpha-channel. Default —.
*
* @return $result {string}
*/
@@ -605,17 +714,19 @@ private static function hsbToHex($paramHsb): string {
/**
* hsbToHsl
- * @version 1.0 (2023-03-10)
+ * @version 1.1 (2024-01-19)
*
* @param $paramHsb {stdClass|arrayAssociative} — Color in HSB format. @required
* @param $paramHsb->h {integer} — Hue. @required
* @param $paramHsb->s {integer} — Saturation. @required
* @param $paramHsb->b {integer} — Brightness. @required
+ * @param $paramHsb->a {integer} — Alpha-channel. Default —.
*
* @return $result {stdClass}
* @return $result->h {integer}
* @return $result->s {integer}
* @return $result->l {integer}
+ * @return $result->a {integer} — Can be absent.
*/
private static function hsbToHsl($paramHsb): \stdClass {
$paramHsb = (object) $paramHsb;
@@ -646,22 +757,38 @@ private static function hsbToHsl($paramHsb): \stdClass {
}
}
+ if (
+ \DDTools\ObjectTools::isPropExists([
+ 'object' => $paramHsb,
+ 'propName' => 'a'
+ ])
+ && $paramHsb->a < 100
+ ){
+ $resultHsl->a = $paramHsb->a;
+
+ if ($resultHsl->a < 1){
+ $resultHsl->a = $resultHsl->a * 100;
+ }
+ }
+
return $resultHsl;
}
/**
* hslToHsb
- * @version 1.0 (2023-03-10)
+ * @version 1.1 (2024-01-19)
*
* @param $paramHsl {stdClass|arrayAssociative} — Color in HSL format. @required
* @param $paramHsl->h {integer} — Hue. @required
* @param $paramHsl->s {integer} — Saturation. @required
* @param $paramHsl->l {integer} — Lightness. @required
+ * @param $paramHsl->a {integer} — Alpha-channel. Default —.
*
* @return $result {stdClass}
* @return $result->h {integer}
* @return $result->s {integer}
* @return $result->b {integer}
+ * @return $result->a {integer} — Can be absent.
*/
private static function hslToHsb($paramHsl): \stdClass {
$paramHsl = (object) $paramHsl;
@@ -692,6 +819,20 @@ private static function hslToHsb($paramHsl): \stdClass {
));
}
+ if (
+ \DDTools\ObjectTools::isPropExists([
+ 'object' => $paramHsl,
+ 'propName' => 'a'
+ ])
+ && $paramHsl->a < 100
+ ){
+ $resultHsb->a = $paramHsl->a;
+
+ if ($resultHsb->a < 1){
+ $resultHsb->a = $resultHsb->a * 100;
+ }
+ }
+
return $resultHsb;
}