Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are going from bright colour to bright colour as the hex values suggest, then you may wish to interpolate in <a href="http://en.wikipedia.org/wiki/HSL_color_space" rel="nofollow noreferrer">HSV space</a>, rather than RGB space. HSV space approximates how we think about colour - hue, saturation and value. RGB space approximates how the light sensitive cells in our eyes work. </p> <p>The upper gradient is linear RGB interpolation from FF0000 to 00FF00. Its middle value is 7f7f00, a muddy brown.</p> <p>The middle gradient is a linear interpolation in HSV space. As both FF0000 and 00FF00 are fully saturated and have the same value (brightness), the interpolation keeps the same brightness and saturation throughout, so the centre value is bright yellow ffff00. </p> <p>The third alternative would be a vector rotation in RGB space, which would mean the mid-value is B4B400, ( B4 hex = 180 dec = 255 / sqrt(2) ), which is somewhere between the two effects. This is done by calculating the magnitude of each end point, then scaling the result of the RGB linear interpolation so it is the same magnitude, effectively sweeping the vector in an arc in the plane of the two colours and the origin. As we don't actually weigh different colours equally for brightness, or see linearly, it's not exact, but it does have a fairly even intensity across its sweep, whereas the HSV one is a bit lighter in the middle, as it's got two values at 100%.</p> <p><em>removed dead Imageshack link</em></p> <hr> <p>In Java, where you have HSB support the algorithm is simple - get the HSB of the end values, linear interpolate them as in the other RGB answers, then convert create a colour with the h,s,v values:</p> <pre><code>static Color hsvInterpolate ( float mix, Color c0, Color c1 ) { float[] hsv0 = new float[3]; float[] hsv1 = new float[3]; float alt = 1.0f - mix; Color.RGBtoHSB( c0.getRed(), c0.getGreen(), c0.getBlue(), hsv0 ); Color.RGBtoHSB( c1.getRed(), c1.getGreen(), c1.getBlue(), hsv1 ); float h = mix * hsv0 [ 0 ] + alt * hsv1 [ 0 ]; float s = mix * hsv0 [ 1 ] + alt * hsv1 [ 1 ]; float v = mix * hsv0 [ 2 ] + alt * hsv1 [ 2 ]; return Color.getHSBColor ( h, s, v ); } </code></pre> <p>I don't believe C# has the conversions built in, so the code isn't much use really.</p> <pre><code>static Color vectorInterpolate ( float mix, Color c0, Color c1 ) { float alt = 1.0f - mix; double x0 = c0.getRed(); double y0 = c0.getGreen(); double z0 = c0.getBlue(); double x1 = c1.getRed(); double y1 = c1.getGreen(); double z1 = c1.getBlue(); double mag0 = sqrt( x0*x0 + y0*y0 + z0*z0 ); double mag1 = sqrt( x1*x1 + y1*y1 + z1*z1 ); double x = mix * x0 + alt * x1; double y = mix * y0 + alt * y1; double z = mix * z0 + alt * z1; double mag = mix * mag0 + alt * mag1; double scale = mag / sqrt( x*x + y*y + z*z ); return new Color ( clamp ( x * scale ), clamp ( y * scale ), clamp ( z * scale ) ); } static int clamp ( double value ) { int x = (int) round ( value ); if ( x &gt; 255 ) return 255; if ( x &lt; 0 ) return 0; return x; } </code></pre> <p>You probably want to find the intersection of the vector with the edge of the RGB cube rather than simply clamping it, but in this case it doesn't matter either way.</p> <hr> <p>As an addendum, it is also worth considering HSY space, which is closer to perceived brightness, as illustrated by <a href="https://www.mrao.cam.ac.uk/~dag/CUBEHELIX/" rel="nofollow noreferrer">Dave Green's cube helix</a> colour interpolations.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload