Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Added colorIsDark twig filter
Browse files Browse the repository at this point in the history
  • Loading branch information
roelvanhintum committed Nov 15, 2018
1 parent c7e8509 commit 730cdaf
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.0-beta.5 - 2018-11-15
### Added
- Added colorIsDark twig filter

## 2.0.0-beta.4 - 2018-11-05
### Fixed
- Fixed a major bug causing the queue to build up.
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "born05/craft-colorextractor",
"description": "Extract colors from image assets in Craft CMS. Requires a field named `imageColor` on all assets of kind image (can be color or plaintext)",
"type": "craft-plugin",
"version": "2.0.0-beta.4",
"version": "2.0.0-beta.5",
"keywords": [
"craft",
"cms",
Expand Down Expand Up @@ -38,7 +38,8 @@
"changelogUrl": "https://raw.githubusercontent.com/born05/craft-colorextractor/master/CHANGELOG.md",
"components": {
"asset": "born05\\colorextractor\\services\\Asset",
"assetUpload": "born05\\colorextractor\\services\\AssetUpload"
"assetUpload": "born05\\colorextractor\\services\\AssetUpload",
"color": "born05\\colorextractor\\services\\Color"
},
"class": "born05\\colorextractor\\ColorExtractor"
}
Expand Down
1 change: 1 addition & 0 deletions src/ColorExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use born05\colorextractor\services\Asset as AssetService;
use born05\colorextractor\services\AssetUpload as AssetUploadService;
use born05\colorextractor\services\Color as ColorService;
use born05\colorextractor\twigextensions\ColorExtractorTwigExtension;

use Craft;
Expand Down
8 changes: 4 additions & 4 deletions src/services/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public function getImageColor(AssetElement $asset, $forceSave = false)
$e->getMessage(),
__METHOD__
);

return false;
}

$asset->setFieldValue('imageColor', $color);
Craft::$app->getElements()->saveElement($asset);
if (!empty($color)) {
$asset->setFieldValue('imageColor', $color);
Craft::$app->getElements()->saveElement($asset);
}
}

// Return color with black fallback.
Expand Down
21 changes: 21 additions & 0 deletions src/services/Color.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace born05\colorextractor\services;

use League\ColorExtractor\Color as LeagueColor;

use Craft;
use craft\base\Component;
use craft\elements\Asset as AssetElement;

class Color extends Component
{
public function isDark(string $color) {
$intColor = LeagueColor::fromHexToInt($color);
$rgbColor = LeagueColor::fromIntToRgb($intColor);

$darkness = 1 - (0.299 * $rgbColor['r'] + 0.587 * $rgbColor['g'] + 0.114 * $rgbColor['b']) / 255;

return $darkness >= 0.5;
}
}
12 changes: 12 additions & 0 deletions src/twigextensions/ColorExtractorTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getFilters()
{
return [
new \Twig_SimpleFilter('colorExtractor', [$this, 'colorExtractorFilter']),
new \Twig_SimpleFilter('colorIsDark', [$this, 'colorIsDarkFilter']),
];
}

Expand All @@ -36,4 +37,15 @@ public function colorExtractorFilter($asset)
{
return ColorExtractor::$plugin->asset->getImageColor($asset);
}

/**
* @param null $asset
*
* @return boolean
*/
public function colorIsDarkFilter($asset)
{
$color = ColorExtractor::$plugin->asset->getImageColor($asset);
return ColorExtractor::$plugin->color->isDark($color);
}
}

0 comments on commit 730cdaf

Please sign in to comment.