Color generation function

Consider the following scenario: a function that can generate code colors from white to red, from white to blue, from white to pink, from white to orange, etc.

The color code is in RGB format with values ​​from 0 to 255.

Any ideas? Can you give me a pseudo-code or a link to such an algorithm?

+3
source share
5 answers

It looks like you are after linear interpolation - in your case, interpolating from white to the specified color. For example, in C #:

public IEnumerable<Color> Interpolate(Color from, Color to, int steps)
{
    int range = steps-1; // Makes things a bit easier
    for (int i=0; i < steps; i++)
    {
        // i is the proportion of the "to" colour to use.
        // j is the proportion of the "from" colour to use.

        int j = range - i;
        int r = ((from.R * j) + (to.R * i)) / range;
        int g = ((from.G * j) + (to.G * i)) / range;
        int b = ((from.B * j) + (to.B * i)) / range;
        yield return new Color(r, g, b);
    }
}

, , , , , . , , , . - 256 , - 255, int s.

EDIT: , RGB , . / RGB HSL HSV . . , .

+12

:

  • RGB
  • RGB (, , , , ).
  • RGB, RGB RGB

, (0,0,0), (0,255,255), (0,128,128)... , (0,64,64).

+3

, . , . ColorBrewer.

, , .

+2

, , R, G B.

, "" .

R, G B . " " .

. - , .

+1

? - , 255 255 255 , 255 0 0. , 0 0 255 ..

0

Source: https://habr.com/ru/post/1708522/


All Articles