markdock/imalette

在预定义调色板中查找图像颜色

v1.1 2024-07-02 09:26 UTC

This package is auto-updated.

Last update: 2024-10-02 10:12:15 UTC


README

在预定义调色板中查找图像颜色。

目录

介绍

Imalette使用“K-means聚类”算法来分析图像颜色,将它们分组并识别最具有代表性的颜色组。

然后,它计算提供的调色板中每种颜色与主导颜色之间的欧几里得距离,以确定最接近的颜色。

安装

  1. 请确保您的系统已安装PHP。GD库对于图像处理是必需的。(http://www.webassist.com/tutorials/Enabling-the-GD-library-setting

  2. 使用composer安装

    cd votre/projet/php
    composer require markdock/imalette

使用

  1. 创建调色板

  2. 使用findColor(palette, image)函数来识别颜色

    <?php
    use Imalette\Imalette;
    use Imalette\Palette;
    
    // Image
    $image = "images/image.png"; // jpg/jpeg, png, gif, bmp, webp
    
    // Palette
    $palette = new Palette();
    $palette->addColorRGB(0, 0, 0); // noir
    $palette->addColorRGB(127, 127, 127); // gris
    $palette->addColorRGB(255, 255, 255); // blanc
    $palette->addColorRGBarray(array(255, 0, 0)); // rouge
    $palette->addColorRGBarray(array(0, 255, 0)); // vert
    $palette->addColorRGBarray(array(0, 0, 255)); // bleu
    $palette->addColorRGBarray(array(255, 255, 0)); // jaune
    $palette->addColorRGBarray(array(255, 0, 255)); // magenta
    $palette->addColorRGBarray(array(0, 255, 255)); // cyan
    $palette->addColorHEX("#FFA500"); // Orange
    $palette->addColorHEX("#800080"); // Violet
    $palette->addColorHEX("#FFC0CB"); // Rose
    $palette->addColorHEX("#A52A2A"); // Marron
    $palette->addColorHEX("#00FF00"); // Lime
    $palette->addColorHEX("#808000"); // Olive
    $palette->addColorHEX("#800000"); // Bordeaux
    $palette->addColorHEX("#000080"); // Bleu marine
    $palette->addColorHEX("#008080"); // Sarcelle
    $palette->addColorHEX("#C0C0C0"); // Argent
    $palette->addColorHEX("#FFD700"); // Or
    
    // Imalette
    $imalette = new Imalette();
    $palcol = $imalette->findColor($palette, $image[, $ignore_color]); // Donne la couleur en HEX (exemple: #808000)
    // $ignore_color est optionnel, sa valeur par défaut est array(250, 250, 250, '>=')
    // Ce paramètre permet d'ignorer la couleur de fond. Les trois premières valeurs sont des valeurs RGB.
    // La 4e valeur correspond à l'attitude à adopter vis-à-vis de cette couleur (ignorer seulement celle-ci ? tout ce qui est supérieur ? etc.): '>', '<', '>=', '<=', '==', '!='.
    // Notons que les pixels invisibles ou trop transparents sont déjà ignorés.
    // $ignore_color peut aussi être égal à -1. Dans ce cas, la couleur du premier pixel de l'image est utilisée pour détecter la couleur de fond et ignorer tous les pixels de la même couleur.
    ?>
  3. 其他函数

    // Palette
    $palette = $palette->getPalette(); // array avec toutes les couleurs en HEX
    $palette = $palette->getColor(3); // récupère une couleur. paramètre id = [ 0 - (n-1) ]
    
    // Imalette
    $colors = $imalette->getColors($image[, $ignore_color]); // array contenant l'ensemble des pixels de l'image en RGB
    $distance = $imalette->getDistance($p1, $p2); // calcule la distance Euclidenne entre deux points
    $hex = $imalette->rgb2hex($rgb); // conversion rgb -> hex
    $rgb = $imalette->hex2rgb($rgb); // conversion hex -> rgb
    $lab = $imalette->rgb2lab($rgb); // conversion rgb -> lab
    $rgb = $imalette->lab2rgb($rgb); // conversion lab -> rgb