Use the median cut algorithm to cluster similar colors.
public static getPalette ( mixed $sourceImage, integer $colorCount = 10, integer $quality = 10, array $area = null ) : array | ||
$sourceImage | mixed | Path/URL to the image, GD resource, Imagick instance, or image as binary string |
$colorCount | integer | It determines the size of the palette; the number of colors returned. |
$quality | integer | 1 is the highest quality. |
$area | array | |
리턴 | array |
/** * Gets color palette for image * * @param AssetFileModel|string $image * @param $colorCount * @param $quality * @param $colorValue * @return array * @throws Exception */ public function getColorPalette($image, $colorCount, $quality, $colorValue) { $pathsModel = new Imager_ImagePathsModel($image); if (!IOHelper::getRealPath($pathsModel->sourcePath)) { throw new Exception(Craft::t('Source folder “{sourcePath}” does not exist', array('sourcePath' => $pathsModel->sourcePath))); } if (!IOHelper::fileExists($pathsModel->sourcePath . $pathsModel->sourceFilename)) { throw new Exception(Craft::t('Requested image “{fileName}” does not exist in path “{sourcePath}”', array('fileName' => $pathsModel->sourceFilename, 'sourcePath' => $pathsModel->sourcePath))); } $palette = ColorThief::getPalette($pathsModel->sourcePath . $pathsModel->sourceFilename, $colorCount, $quality); return $colorValue == 'hex' ? $this->_paletteToHex($palette) : $palette; }
/** * @see Issue #11 * @expectedException RuntimeException * @expectedExceptionMessage blank or transparent image * @expectedExceptionCode 1 */ public function testGetPaletteWithBlankImage() { ColorThief::getPalette($this->getBlankImage()); }
/** * Get the dominant colors of this Image * @param Int $colorCount Count of colors to return * @return Array */ public function dominantColorPalette($colorCount = 5) { $c = ColorThief::getPalette($sourceImage = $this->owner->getFullPath(), $colorCount = $colorCount, $quality = Config::inst()->get(get_called_class(), 'quality')); return array_map(array(get_called_class(), 'array_to_hex'), $c); }
public function actionUpload() { $file = UploadedFile::getInstanceByName('file'); if ($file) { $destName = time() . '_' . $file->name; $targetFile = \Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . '_lib' . DIRECTORY_SEPARATOR . $destName; $file->saveAs($targetFile); $filePath = \Yii::getAlias('@app/workspace/elements') . DIRECTORY_SEPARATOR . 'asset' . '.json'; $elementDefinition = Json::decode(file_get_contents($filePath), false); // Add core fields. $elementDefinition->fields[] = Json::decode('{ "label": "UUID", "key": "uuid", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]], "options": {"disabled":true}}', false); $elementDefinition->fields[] = Json::decode('{ "label": "Path", "key": "path", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]]}', false); $elementDefinition->fields[] = Json::decode('{ "label": "Slug", "key": "slug", "type": "textInput", "visibleInGrid": true, "rules": [["string", {"max": 128}]]}', false); $elementDefinition->fields[] = Json::decode('{ "label": "State", "key": "state", "type": "dropDownList", "visibleInGrid": true, "rules": [["required"], ["string", {"max": 128}]], "options": {"prompt":"Please set state"}, "items": {"0":"Offline", "1":"Draft", "2":"Online", "3":"Archived"}}', false); $fields = []; foreach ($elementDefinition->fields as $field) { array_push($fields, $field->key); } $model = new CrelishDynamicJsonModel($fields); $model->identifier = 'asset'; $model->systitle = $destName; $model->title = $destName; $model->src = \Yii::getAlias('@web') . '/' . '_lib' . '/' . $destName; $model->mime = $file->type; $model->size = $file->size; $model->state = 2; $model->save(); try { $domColor = ColorThief::getColor($targetFile, 20); $palColor = ColorThief::getPalette(\Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . '_lib' . DIRECTORY_SEPARATOR . $destName); $model->colormain_rgb = Json::encode($domColor); $model->colormain_hex = '#' . sprintf('%02x', $domColor[0]) . sprintf('%02x', $domColor[1]) . sprintf('%02x', $domColor[2]); $model->save(); $model->colorpalette = Json::encode($palColor); $model->save(); } catch (Exception $e) { \Yii::$app->session->setFlash('secondary', 'Color theft could not be completed. (Image too large?)'); } } return false; }
/** * @see Issue #11 * @expectedException \RuntimeException * @expectedExceptionMessage blank or transparent image * @expectedExceptionCode 1 */ public function testGetPaletteWithBlankImage() { ColorThief::getPalette(__DIR__ . "/images/blank.png"); }