IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[ImageMagick] Image color palette


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Exo
    Exo est déconnecté
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2004
    Messages
    26
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2004
    Messages : 26
    Points : 20
    Points
    20
    Par défaut [ImageMagick] Image color palette
    Coucou a tous,

    Une petite question pour vous.

    J'ai fais une fonction pour faire une palette de couleur d'après une image.
    Voila ou je coince, j'obtiens pliens de couleurs différentes, mais quand meme très proche les une de les autres.

    Comment est ce que je pourrais faire pour obtenir par example les X couleurs les plus dominantes d'une image ?

    Voici ma fonction


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
    function colorPalette( $img ) {
    	$imgname = $img;
    	$im = imagecreatefromjpeg($imgname);
    	$x = imagesx($im);
    	$y = imagesx($im);
     
    	$start_x = 1;
    	$start_y = 1;
    	$palette = array();
     
    	while ( $start_x <= $x && $start_y <= $y  ) {
    		$color = ImageColorAt($im, $start_x, $start_y);
    		$r = ($color >> 16) & 0xFF;
    		$g = ($color >> 8) & 0xFF;
    		$b = $color & 0xFF;
     
    		$rgbtohex = dechex($color);	
     
    		if( strlen( $rgbtohex ) > 2 ) {
    			if( array_key_exists( $rgbtohex, $palette ) )
    				$palette[$rgbtohex]++;
    			else
    				$palette[$rgbtohex] = 1;
    		}
     
    		if( $start_x == $x ) {
    			$start_y++;
    			$start_x = 1;	
    		}
     
    		$start_x++;
     
    	}
     
    	arsort($palette);
     
    	return  $palette;
    }
     
    // On affiche
    $colors = colorPalette( $imgname );
    $format = '<div style="background-color: #%s; height:32px; width:66px;"></div>';
    $res = '';
    foreach ( $colors as $color => $nb) {
    	$res .= sprintf( $format, $color );
    }
    print $res;

    Si vous avez une idée....
    Merci !

  2. #2
    Rédacteur

    Avatar de Yogui
    Homme Profil pro
    Directeur technique
    Inscrit en
    Février 2004
    Messages
    13 721
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yonne (Bourgogne)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Février 2004
    Messages : 13 721
    Points : 29 985
    Points
    29 985
    Par défaut
    Salut

    Je n'ai pas d'idée (ça devient trop mathématique pour moi) mais peut-être peux-tu t'inspirer des sujets suivants :
    Détection des couleurs approximative d'une image??
    Couleur dominante d'une image

    Il est même éventuellement possible de faire du post revival

    Bonne chance

  3. #3
    Exo
    Exo est déconnecté
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2004
    Messages
    26
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2004
    Messages : 26
    Points : 20
    Points
    20
    Par défaut
    Merci de ta réponse.

    J'ai trouver une solution, ca fonctionne bien

  4. #4
    Rédacteur

    Avatar de Yogui
    Homme Profil pro
    Directeur technique
    Inscrit en
    Février 2004
    Messages
    13 721
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yonne (Bourgogne)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Février 2004
    Messages : 13 721
    Points : 29 985
    Points
    29 985
    Par défaut
    Pourrais-tu nous en faire part, s'il te plaît ? C'est toujours intéressant d'avoir les réponses aux nouvelles questions.
    Pense au bouton [Résolu] ^_^

  5. #5
    Exo
    Exo est déconnecté
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2004
    Messages
    26
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Février 2004
    Messages : 26
    Points : 20
    Points
    20
    Par défaut
    Fichier index.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
     
    <style> 
    .smini {margin:0;float: left;padding:0;height: 12px;width: 12px;}
    </style>
    <?php
    require_once( 'ColorChip.class.php' );
    $imgname = "ROOT_IMG_PATH";
    $img_web = "WEB_IMG_PATH";
     
    function colorPalette( $img ) {
     
    	$imgname = $img;
    	$im = imagecreatefromjpeg($imgname);
    	$x = imagesx($im);
    	$y = imagesx($im);
     
    	$start_x = 1;
    	$start_y = 1;
    	$palette = array();
     
    	while ( $start_x <= $x && $start_y <= $y  ) {
    		$color = ImageColorAt($im, $start_x, $start_y);
    		$r = ($color >> 16) & 0xFF;
    		$g = ($color >> 8) & 0xFF;
    		$b = $color & 0xFF;
     
    		$rgbtohex = dechex($color);
    		$userColor = strtoupper($_GET['userColor']);
    		$color2 = new ColorChip($rgbtohex, null, null, CC_HEX);
    		$webSafe = $color2->getNearestWebSafe();
     
    		if( array_key_exists( $webSafe->hex, $palette ) ) {
    			$palette[$webSafe->hex]['count']++;
    		} else {
    			$palette[$webSafe->hex]['count'] = 1;
    			$palette[$webSafe->hex]['r'] = $webSafe->r;
    			$palette[$webSafe->hex]['g'] = $webSafe->g;
    			$palette[$webSafe->hex]['b'] = $webSafe->b;
    			$palette[$webSafe->hex]['h'] = $webSafe->h;
    			$palette[$webSafe->hex]['s'] = $webSafe->s;
    			$palette[$webSafe->hex]['v'] = $webSafe->v;
    		}
     
    		if( $start_x == $x ) {
    			$start_y++;
    			$start_x = 1;	
    		}
     
    		$start_x++;
     
    	}
     
    	return  $palette;
    }
    $colors = colorPalette( $imgname );
    echo "<p><img src=\"$img_web\" ></img></p>";
    foreach ( $colors as $color => $infos ) {
    	$linkColor = ($infos['v'] > 60 ? "000000" : "FFFFFF");
    	echo "<div class=\"smini\" style='background-color:#$color;padding:.5em'></div>";
    }
     
    ?>
    Et la class que j'ai trouver

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
     
    <?php
    /**
    ColorChip - A PHP class for manipulating color.
    
    Copyright (C) 2002  Andy Chase
    <achase@greyledge.net>
    http://andy.greyledge.net
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    */
     
    /** A constant used to indicate that RGB color information is to be used by 
     *certain methods. */
    define('CC_RGB', 'RGB');
    /** A constant used to indicate that HSV color information is to be used by 
     *certain methods. */
    define('CC_HSV', 'HSV');
    /** A constant used to indicate that Hexadecimal color information is to be
     *used by certain methods. */
    define('CC_HEX', 'HEX');
    /** The CC_WEBSAFE_ALT constant is used by the getWebSafeDither and 
     *getNearestWebSafeComponent method. 
     *@see getWebSafeDither,getNearestWebSafeComponent 
     */
    define('CC_WEBSAFE_ALT', 'WEBSAFE_ALT');
     
    /**
     *ColorChip is a class for working with color in a more convenient manner 
     *than the basic RGB and Hexadecimal triplets often encountered in programming, 
     *especially web-oriented programming.  ColorChip allows you to define a color 
     *using RGB, HSV (Hue, Saturation, Value) or a Hexadecimal string.  ColorChip 
     *objects have properties for all three of these color models, as well as
     *methods for adjusting both RGB and HSV values - these methods automatically
     *update all of the object's properties.  Additional methods exist for getting
     *color compliments, triads, and the nearest websafe color.
     *@author Andy Chase <achase@greyledge.net> 
     *@link http://andy.greyledge.net
     *@copyright 2002
     */
    class ColorChip{
     
      /** The object's Hue property.  Value can be 0-360, or boolean FALSE for
       * no hue (Grayscale) 
       *@var int
       */
      var $h; 
      /** The object's Saturation property.  Value can be 0-100.
       *@var int
       */
      var $s; 
      /** The object's Value (Brightness) property. Value can be 0-100. 
       *@var int
       */
      var $v; 
      /** The object's Red property.  Value can be 0-255. 
       *@var int
       */
      var $r; 
      /** The object's Green property.  Value can be 0-255. 
       *@var int
       */
      var $g; 
      /** The object's Blue property.  Value can be 0-255. 
       *@var int
       */
      var $b; 
      /** The object's Hex property, formatted as an HTML-style triplet; 
       *E.G., White would be stored as 'FFFFFF' 
       *@var string
       */
      var $hex; 
     
      /** The class constructor.  
       *Arguments one, two, and three are typically the R,G, and B values of 
       *the color chip being defined.  An optional fourth parameter indicates what
       *types of values are being passed.  Default is RGB, but HSV and Hexadecimal 
       *definitions are accepted too.
       *@return void
       *@param [int|string] $arg1 
       *@param [int|null] $arg2
       *@param [int|null] $arg3
       *@param string $type The type of color data being passed to the constructor.
       *This should be one of the constants CC_RGB, CC_HSV, or CC_HEX.  
       *Default is CC_RGB (Where $arg1 is R, $arg2 is G, and $arg3 is B.) 
       *If $type is set to CC_HEX, then $arg1 should contain the full hex triplet 
       *(EG 'FFFFFF'), while $arg2 and $arg3 can be null.
       *@access public
       */
      function ColorChip($arg1, $arg2, $arg3, $type = CC_RGB){
        switch($type){
        case CC_HSV:
          $this->h = $arg1;
          $this->s = $arg2;
          $this->v = $arg3;
          $this->_updateAllColorInfo(CC_HSV);
          break;
     
        case CC_RGB:
          $this->r = $arg1;
          $this->g = $arg2;
          $this->b = $arg3;
          $this->_updateAllColorInfo(CC_RGB);
          break;
     
        case CC_HEX:
          $this->hex = $arg1;
          $this->_updateAllColorInfo(CC_HEX);
          break;
        }
     
      }
     
     
      //-- Public Methods: -------------------------------------------------------------------------
     
      /** Adjusts the object's r property by $amount
       *@param int $amount The amount by which to adjust the object's red component.
       *@return void
       *@access public
       */
      function adjRed($amount){
        $this->r += round($amount);
        $this->_forceRange(0, 255, $this->r);
        $this->_updateAllColorInfo(CC_RGB);
     
      }
     
      /** Adjusts the object's r property by $amount
       *@param int $amount The amount by which to adjust the object's green component.
       *@return void
       *@access public
      */
      function adjGreen($amount){
        $this->g += round($amount);
        $this->_forceRange(0, 255, $this->g);
        $this->_updateAllColorInfo(CC_RGB);
      }
     
      /** Adjusts the object's b property by $amount
       *@param int $amount The amount by which to adjust the object's blue component.
       *@return void
       *@access public
      */
      function adjBlue($amount){
        $this->b += round($amount);
        $this->_forceRange(0, 255, $this->b);
        $this->_updateAllColorInfo(CC_RGB);
      }
     
     
      /** Adjusts the object's h property by $amount
       *@param int $amount The amount to adjust the object's hue. Values above
       *360 or below 0 will be wrapped around to the other end of the scale.  
       *@return void
       *@access public
      */
      function adjHue($amount){
        $this->h += round($amount);
        if($this->h < 0){
          $this->h += 360;
        }
        if($this->h > 360){
          $this->h -= 360;
        }
     
        $this->_updateAllColorInfo(CC_HSV);
     
      }
      /** Adjusts the object's s property by $amount
       *@param int $amount The amount by which to adjust the object's saturation.
       *@return void
       *@access public
      */
      function adjSaturation($amount){
        $this->s += round($amount);
        $this->_forceRange(0, 100, $this->s);
     
        $this->_updateAllColorInfo(CC_HSV);
     
      }
      /** Adjusts the object's v property by $amount
       *@param int $amount The amount by which to adjust the object's value.
       *@return void
       *@access public
      */
      function adjValue($amount){
        $this->v += round($amount);
        $this->_forceRange(0, 100, $this->v);
        $this->_updateAllColorInfo(CC_HSV);
     
      }
     
      /** Returns a new ColorChip object with identical h,s,v,r,g, b and 
       *hex properties.
       *@return ColorChip 
       *@access public
       */
      function clone(){
        $newColor = new ColorChip($this->r, $this->g, $this->b, CC_RGB);
        return $newColor;
      }
     
     
     
      /** Returns a new ColorChip object containing the complimentary color 
       *to the current
       *@return ColorChip The complimentary color
       *@access public
      */
      function getComplementary(){
        $nColor = $this->clone();
        $nColor->adjHue(180);
        return $nColor;
      }
     
     
      /** Returns an array containing two ColorChip objects which form 
       *a color triad with the original.
       *@return array An array containing two ColorChip objects forming a color triad with the original.
       *@access public
      */
      function getTriad(){
        $n1 = $this->clone();
        $n1->adjHue(-120);
        $n2 = $this->clone();
        $n2->adjHue(120);
        return array($n1, $n2);
      }
     
     
      /** Returns a new ColorChip object containing the nearest web safe color
       *to the original.
       *@return ColorChip A new ColorChip object with RGB properties set to the nearest web safe color.
       *@access public
      */
     
      function getNearestWebSafe(){
        $newCol = $this->clone();
        $newCol->r = ColorChip::getNearestWebSafeComponent($newCol->r);
        $newCol->g = ColorChip::getNearestWebSafeComponent($newCol->g);
        $newCol->b = ColorChip::getNearestWebSafeComponent($newCol->b);
        $newCol->_updateHsv();
        $newCol->_updateHex();
        return $newCol;
      }
     
     
      /** Takes a value from 0 - 255 and calculates the nearest web-safe value.  
       *If $which is set to CC_WEBSAFE_ALT, the second-closest web safe value will
       *be returned; this functionality is made available to calculate web-safe
       *dithering. $which is empty by default.
       *@param int $rgbComponent A value from 0-255 that you want to find the nearest web safe value of.
       *@param string $which Which websafe match to return; by default, the method returns the closest websafe match, but if $which is set to CC_WEBSAFE_ALT, the method will return the second closest match; this is useful when attempting to approximate a color by dithering two web-safe colors.
       *@return int The nearest websafe value to $rgbComponent (Will be one of: 0, 51, 102, 153, 204, 255)
       *@access public
       *@see getWebSafeDither
      */
      function getNearestWebSafeComponent($rgbComponent, $which = ''){
        $safe = array(0, .2, .4, .6, .8. 1);
        $pairs = array(
    		   array(0, .2),
    		   array(.2, .4),
    		   array(.4, .6),
    		   array(.6, .8),
    		   array(.8, 1)
    		   );
        $comp = $rgbComponent / 255; //Get RGB component percentage value
     
        if(!in_array($comp, $safe)){
          foreach($pairs as $pair){
    	if($pair[0] < $comp && $comp < $pair[1]){ //If the component is between the current pair of websafe percentages:
    	  if($comp - $pair[0] > $pair[1] - $comp){
    	    if($which == CC_WEBSAFE_ALT){ //If the alternative websafe color is desired:
    	      $comp = $pair[0];
    	    }else{
    	      //Component is closer to high end of web safe pair; set it to match
    	      $comp = $pair[1];
    	    }
    	  }else{
    	    if($which == CC_WEBSAFE_ALT){//If the alternative websafe color is desired:
    	      $comp = $pair[1];
    	    }else{
    	      //Component is closer to low end of web safe pair; set it to match
    	      $comp = $pair[0]; 
    	    }
    	  }
    	}
          }
        }//Component is already web safe; leave it alone.
     
        return $comp * 255;
     
      }
     
      /** Returns an array containing the two web safe colors closest to the
       *actual color represented by the current ColorChip.
       *@return array An array containing the two ColorChip objects of the closest web safe colors to the original
       *@see RenderWebSafeDither
       *@access public
       */
      function getWebSafeDither(){
        $color1 = $this->getNearestWebSafe();
        $color2 = $this->clone();
        $color2->r = ColorChip::getNearestWebSafeComponent($color2->r, CC_WEBSAFE_ALT);
        $color2->g = ColorChip::getNearestWebSafeComponent($color2->g, CC_WEBSAFE_ALT);
        $color2->b = ColorChip::getNearestWebSafeComponent($color2->b, CC_WEBSAFE_ALT);
        $color2->_updateHsv();
        $color2->_updateHex();
        return array($color1, $color2);
      }
     
     
     
      /** Saves a 2 pixel by two pixel dither of the two nearest web-safe colors
       *in the filename specified. If no filename is given, the filename defaults 
       *to 'xxxxxx_websafe.png'; in other words, white would be saved as 
       *'ffffff_websafe.png'.  Requires that PHP be compiled with GD and PNG support.
       *@param string filename The name of the file to save the dither pattern as.
       *@return string $filename The name of the file the background was saved as.
       *@access public
      */
      function renderWebSafeDither($filename = ''){
        if($filename == ''){
          $filename = strtolower($this->hex) . '_websafe.png';
        }
        $safe = $this->getWebSafeDither();
        $im = imagecreate(2,2);
     
        $col1 = imagecolorallocate($im, $safe[0]->r, $safe[0]->g, $safe[0]->b);
        $col2 = imagecolorallocate($im, $safe[1]->r, $safe[1]->g, $safe[1]->b);
        imageSetPixel($im, 0, 0, $col1);
        imageSetPixel($im, 1, 1, $col1);
        imageSetPixel($im, 0, 1, $col2);
        imageSetPixel($im, 1, 0, $col2);
        imagepng($im, $filename);
        return $filename;
      }
     
     
      /** Sets new values for the ColorChip's hue, saturation, and value properties and
       *automatically updates the r,g,b and hex values accordingly.
       *@return void
       *@param int $newH The new hue
       *@param int $newS The new saturation
       *@param int $newV The new value (brightness)
       *@access public
     */
      function setHsv($newH, $newS, $newV){
        $this->h = $newH;
        $this->s = $newS;
        $this->v = $newV;
        $this->_updateAllColorInfo(CC_HSV);
      }
     
      /** Sets new values for the ColorChip's red, green, and blue properties and
       *automatically updates the h,s,v and hex values accordingly.
       *@return void
       *@param int $newR The new red
       *@param int $newG The new green
       *@param int $newB The new blue
       *@access public
      */
      function setRgb($newR, $newG, $newB){
        $this->r = $newR;
        $this->g = $newG;
        $this->b = $newB;
        $this->_updateAllColorInfo(CC_RGB);
      }
     
      /** Sets a new value for the ColorChip's hex property and automatically 
       *updates the h, s, v, r, g, and b values accordingly.
       *@return void
       *@param int $newHex
       *@access public
      */
      function setHex($newHex){
        $this->hex = $newHex;
        $this->_updateAllColorInfo(CC_HEX);
      }
     
     
      //-- Private Methods: ------------------------------------------------------------------------
     
      /** Forces $property to the range specified by $min and $max
       *@return void
       *@param int $min The minimum value for $property Default is 0.
       *@param int $max The maximum value for $property Default is 255.
       *@param int $property The property to force into range. Note that $property is passed by reference.
       *@access private
      */
      function _forceRange($min = 0, $max = 255, &$property){
        if($property < $min){
          $property = $min;
        }
        if($property > $max){
          $property = $max;
        }
      }
     
     
      /** Converts $number to hexadecimal, adding leading 0 if $number is less than 16
       *@return string
       *@param int $number The number to be converted to hexadecimal
       *@access private
     */ 
      function _toHex($number){
        $hex = '';
        if($number < 16){
          $hex = '0';
        }
        $hex .= strtoupper(base_convert($number, 10, 16));
        return $hex;
      }
     
     
      /** Updates all object color information based on the color value specified by
       *the $useInfo parameter.
       *@param string $useInfo
       *@access private
       */
     
      function _updateAllColorInfo($useInfo){
        switch($useInfo){
        case CC_HSV: //Update RGB and Hex values using the contents of h,s, and v
          $this->_updateRgb();
          $this->_updateHex();
          break;
     
        case CC_RGB: //Update HSV and Hex values using the contents of R,G, and B
          $this->_updateHex();
          $this->_updateHsv();
          break;
     
        case CC_HEX: //Update RGB and HSV values using the contents of Hex
          $this->_updateRgb(CC_HEX);
          $this->_updateHsv();
          break;
        }
      }
     
     
      /** Updates the object's r, g, and b properties. if $useInfo is CC_HSV, uses 
       *the object's HSV values, if $useInfo is CC_HEX, uses the object's hex property.
       *@return void
       *@access private
       */
      function _updateRgb($useInfo = CC_HSV){
     
        switch($useInfo){
     
        case CC_HSV:
     
          //Get decimal values (Between 0 and 1) for Hue and Saturation
     
          $value = $this->v / 100;
          $saturation = $this->s / 100;
     
          if($this->h == false){
    	//Undefined hue means grey.  Adjust value to RGB 0-255 range and return
    	$value = floor($value * 255);
    	$this->r = $value;
    	$this->g = $value;
    	$this->b = $value;
          }else{
     
    	$hue = $this->h / 60; //Get local value of hue, ranging from 1-6
    	$i = floor($hue);
    	$f = $hue - $i;
    	if(!($i & 1)){ //(If $i is even)
    	  $f = 1 - $f;
    	}
     
    	$m = $value * (1 - $saturation);
    	$n = $value * (1 - $saturation * $f);
     
     
    	//Adjust $value, $m, and $n to 0-255 scale
     
    	$value = floor(255 * $value);
    	$m = floor(255 * $m);
    	$n = floor(255 * $n);
     
    	switch($i){
    	case 6: 
    	case 0: 
    	  $this->r = $value;
    	  $this->g = $n;
    	  $this->b = $m;
    	  break;
    	case 1: 
    	  $this->r = $n;
    	  $this->g = $value;
    	  $this->b = $m;
    	  break;
    	case 2: 
    	  $this->r = $m;
    	  $this->g = $value;
    	  $this->b = $n;
    	  break;
    	case 3:
    	  $this->r = $m;
    	  $this->g = $n;
    	  $this->b = $value;
    	  break;
    	case 4:
    	  $this->r = $n;
    	  $this->g = $m;
    	  $this->b = $value;
    	  break;
    	case 5: 
    	  $this->r = $value;
    	  $this->g = $m;
    	  $this->b = $n;
    	  break;
    	}
          }
     
          break;
     
        case CC_HEX:
     
          $this->r = base_convert(substr($this->hex,0,2), 16, 10);
          $this->g = base_convert(substr($this->hex,2,2), 16, 10);
          $this->b = base_convert(substr($this->hex,4,2), 16, 10);
     
          break;
     
        }
      }
     
     
     
      /** Updates the h, s, and v object properties based on the object's r, g, and b values.
       *@return void
       *@access private
       */
      function _updateHsv(){
        $max = max($this->r,$this->g,$this->b);
        $min = min($this->r,$this->g,$this->b);
        $delta = $max-$min;
        $this->v = round(($max / 255) * 100);
        if($max != 0){
          $this->s = round($delta/$max * 100);
        }else{
          $this->s = 0;
        }
     
        if($this->s == 0){
          $this->h = false;
        }else{
          if($this->r == $max){
    	$this->h = ($this->g - $this->b) / $delta;
          }elseif($this->g == $max){
    	$this->h = 2 + ($this->b - $this->r) / $delta;
          }elseif($this->b == $max){
    	$this->h = 4 + ($this->r - $this->g) / $delta;
          }
          $this->h = round($this->h * 60);
          if($this->h > 360){
    	$this->h = 360;
          }
          if($this->h < 0){
    	$this->h += 360;
          }
        }
      }
     
      /** Updates the object's hex property based on its r, g, and b values. 
       *@return void
       *@access private
       */
      function _updateHex(){
        $this->hex = $this->_toHex($this->r) . $this->_toHex($this->g) . $this->_toHex($this->b);
      }
    }
     
    ?>
    Voila voila, j'ai pas eu le temps de faire une class ! dsl

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [ImageMagick] Image, texte et liens hypertexte
    Par gailup dans le forum Bibliothèques et frameworks
    Réponses: 9
    Dernier message: 19/05/2006, 16h28
  2. [ImageMagick] Image dynamique et les include
    Par xtaze dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 26/04/2006, 16h33
  3. [Image]Convertir couleurs images avec palette donnée
    Par matique dans le forum Bibliothèques et frameworks
    Réponses: 5
    Dernier message: 08/02/2006, 17h51
  4. [ImageMagick] Image ne pouvant être affichée car elle contient des erreurs
    Par hutchuck dans le forum Bibliothèques et frameworks
    Réponses: 5
    Dernier message: 09/12/2005, 13h59
  5. [ImageMagick] Images miniatures
    Par oli2a dans le forum Bibliothèques et frameworks
    Réponses: 5
    Dernier message: 08/11/2004, 14h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo