Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6850295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:06:24+00:00 2026-05-27T01:06:24+00:00

I’m interesting in algorithms to generate ‘n’ graduated colors between two given colors, that

  • 0

I’m interesting in algorithms to generate ‘n’ graduated colors between two given colors, that generate smooth transitions between each of them.

I tried letting static two channels, for example R and G, and incremental change B, but sometimes the difference between two colors are harder than the neighbors have.

I want to check different algorithms and analyze their weakness and strenghts.


I wrote this code and it seems logic, but the transitions between some colors are harder than between other (e.g.between 0 and 1 is harder than between 1 and 2):

<?php
$c1 = array(128,175,27); // Color 1
$c2 = array(255,255,140); // Color 2
$nc = 5; // Number of colors to display.
$dc = array(($c2[0]-$c1[0])/($nc-1),($c2[1]-$c1[1])/($nc-1),($c2[2]-$c1[2])/($nc-1)); // Step between colors

for ($i=0;$i<$nc;$i++){
    echo '<div style="width:200px;height:50px;background-color:rgb('.round($c1[0]+$dc[0]*$i).','.round($c1[1]+$dc[1]*$i).','.round($c1[2]+$dc[2]*$i).');">'.$i.'</div>'; // Output
}
?>

Are there a better algorithm to do this?


I bring an example: In the code above I used $c1=array(192,5,248); and $c2 = array(142,175,240); and $nc = 10; and obtained this image:

graduated colors example

The RGB values of 0,1,8 and 9 are:

  • 0 = 192,5,248
  • 1 = 186,24,247
  • 8 = 148,156,241
  • 9 = 142,175,240

If you look there is a diference between neighbor colors of 6,19,1. But the visually transition between 0 and 1 is softer than the transition between 8 and 9. And for HSV is the same thing. It is something with some colors that do its transition harder or softer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:06:25+00:00Added an answer on May 27, 2026 at 1:06 am

    In the following image you can see the output of a piece of code I wrote to compare transitions between two colors using RGB and HSV splitting in equal size steps:

    enter image description here

    I found the transitions using HSV are afected by Hue and depend of the distance between colors. If you choose two colors with the same hue is interesting to see that HSV transitions are more clear than in RGB, because you are only playing with saturation and value (black) and no adding colors like in RGB.

    <?php
    // Configuration.
    $nc = 6; // Number of colors.
    $w = 300; // Width of divs.
    $a = 50; // Height of divs.
    
    // Colors
    /* In RGB */
    $c1 = array(rand(0,255),rand(0,255),rand(0,255));
    $c2 = array(rand(0,255),rand(0,255),rand(0,255)); 
    //$c1 = array(128,175,27); // Color 1: Whit these colors is not change.
    //$c2 = array(255,255,140); // Color 2: Whit these colors is not change.
    // $c1 = array(0,0,0); // Color 1: White.
    // $c2 = array(255,255,255); // Color 2: Black.
    /* In HSV */
    $h3 = array(rand(0,360),rand(0,100),rand(0,100)); 
    $h4 = array(rand(0,360),rand(0,100),rand(0,100)); 
    //$h3 = array(145,50,50); // Color 3: To see the influence of Hue.
    //$h4 = array(145,0,100); // Color 4: To see the influence of Hue.
    
    // HTML
    $html .= '<div style="margin:auto;width:'.($w*2).'px;">';
    // RGB to RGB split
    $c = graduateRGB($c1,$c2,$nc);
    $html .= customHTML($w,$a,$c,'RGB->RGBs');
    // RGB to HSV split
    $h1 = RGBtoHSV($c1);
    $h2 = RGBtoHSV($c2);
    $h = graduateHSV($h1,$h2,$nc);
    $html .= customHTML($w,$a,$h,'RGB->HSVs');
    // HSV to HSV split
    $h = graduateHSV($h3,$h4,$nc);
    $html .= customHTML($w,$a,$h,'HSV->HSVs');
    // HSV to RGB split
    $c3 = HSVtoRGB($h3);
    $c4 = HSVtoRGB($h4);
    $c = graduateRGB($c3,$c4,$nc);
    $html .= customHTML($w,$a,$c,'HSV->RGBs');
    // Output
    $html .= '</div>';
    echo $html;
    
    /* FUNCIONES DE GRADUACIÓN */
    // Dados dos colores RGB (0-255,0-255,0-255) y un número de colores deseados, regresa un array con todos los colores de la gradación.   
    function graduateRGB($c1,$c2,$nc){
        $c = array();
        $dc = array(($c2[0]-$c1[0])/($nc-1),($c2[1]-$c1[1])/($nc-1),($c2[2]-$c1[2])/($nc-1));
        for ($i=0;$i<$nc;$i++){
            $c[$i][0]= round($c1[0]+$dc[0]*$i);
            $c[$i][1]= round($c1[1]+$dc[1]*$i);
            $c[$i][2]= round($c1[2]+$dc[2]*$i);
        }
        return $c;
    }
    // Dados dos colores HSV (0-360,0-100,0-100) y un número de colores deseados, regresa un array con todos los colores de la gradación en RGB.    (Hay un detalle con esta función y es que la transición se podría hacer por el lado contrario del círculo cromático)
    function graduateHSV($h1,$h2,$nc){
        $h = array();
        $dh = array(($h2[0]-$h1[0])/($nc-1),($h2[1]-$h1[1])/($nc-1),($h2[2]-$h1[2])/($nc-1));
        for ($i=0;$i<$nc;$i++){
            $h[$i][0]= $h1[0]+$dh[0]*$i;
            $h[$i][1]= $h1[1]+$dh[1]*$i;
            $h[$i][2]= $h1[2]+$dh[2]*$i;
            $h[$i] = HSVtoRGB($h[$i]);
        }
        return $h;
    }
    
    /* FUNCIONES DE CONVERSIÓN. */
    // Convierte a HSV (0-360,0-100,0-100) colores en RGB (0-255,0-255,0-255).
    function RGBtoHSV(array $rgb) {
    
        $f = 0.00000001; // Factor de corrección para evitar la división por cero.
    
        list($R,$G,$B) = $rgb;
    
        $R = $R==0?$f:$R/255;
        $G = $G==0?$f:$G/255;
        $B = $B==0?$f:$B/255;
    
        $V = max($R,$G,$B);
        $X = min($R,$G,$B);
        $S = ($V-$X)/$V;
    
        $V_X = $V-$X==0?$f:$V-$X;
    
        $r = ($V-$R)/($V_X);
        $g = ($V-$G)/($V_X);
        $b = ($V-$B)/($V_X);    
    
        if ($R == $V)
            $H = $G==$X?(5+$b):(1-$g);
        elseif ($G == $V)
            $H = $B==$X?(1+$r):(3-$b);
        else
            $H = $R==$X?(3+$g):(5-$r);
    
        $H /= 6;
    
        $H = round($H*360);
        $S = round($S*100);
        $V = round($V*100);
    
        return array($H, $S, $V);
    }
    
    // Convierte a RGB (0-255,0-255,0-255) colores en HSV (0-360,0-100,0-100).
    function HSVtoRGB(array $hsv) {
        list($H,$S,$V) = $hsv;
    
        $H = $H/360;
        $S = $S/100;
        $V = $V/100;
    
        //1
        $H *= 6;
        //2
        $I = floor($H);
        $F = $H - $I;
        //3
        $M = $V * (1 - $S);
        $N = $V * (1 - $S * $F);
        $K = $V * (1 - $S * (1 - $F));
        //4
        switch ($I) {
            case 0:
                list($R,$G,$B) = array($V,$K,$M);
                break;
            case 1:
                list($R,$G,$B) = array($N,$V,$M);
                break;
            case 2:
                list($R,$G,$B) = array($M,$V,$K);
                break;
            case 3:
                list($R,$G,$B) = array($M,$N,$V);
                break;
            case 4:
                list($R,$G,$B) = array($K,$M,$V);
                break;
            case 5:
            case 6: //for when $H=1 is given
                list($R,$G,$B) = array($V,$M,$N);
                break;
        }
    
        $R = round($R*255);
        $G = round($G*255);
        $B = round($B*255);
    
        return array($R, $G, $B);
    }
    
    // Función con un HTML de muestra para la visualización de colores, podría ser cualquier otro.
    function customHTML($w,$a,$c,$header){
        $html = '<div style="float:left;text-align:center;"><h2>'.$header.'</h2>';
        foreach ($c as $color){
            $html .= '<div style="width:'.$w.'px;height:'.$a.'px;background-color:rgb('.$color[0].','.$color[1].','.$color[2].');">RGB '.$color[0].','.$color[1].','.$color[2].'</div>';
        }
        $html .= '</div>';
        return $html;
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.