Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To scale a color correctly, you have to multiply each RGB value by a proportion. <code>E.g.</code>, if your color is <code>#00417b</code> and you want a color that is 125% lighter color then you have to do this:</p> <pre><code>var dark = {r: 0, g: 65, b: 123}; var light = {r: Math.round(Math.min(dark[r]*1.25, 255)), g: Math.round(Math.min(dark[g]*1.25, 255)), b: Math.round(Math.min(dark[b]*1.25, 255))}; </code></pre> <p>Compare the result for yourself: dark is <code>#00417b</code>, and light is <code>#00519A</code>, although it's perfectly valid CSS to describe them as <code>rgb(0, 65, 123)</code> and <code>rgb(0, 81, 154)</code> and probably easier too. By scaling colors in this way they will appear to be at the same level of saturation, something that simply adding or subtracting numbers will not achieve.</p> <p>Be aware that since values are clamped at [0, 255], if you keep shifting colors, then feeding them back into this process, you can destroy information about the proportion of red, green and blue in the source color. For this reason, keep the original color saved and try to use that as your input each time.</p> <p>Since your question asked specifically about gradients though, this is how you would go between two color values:</p> <pre><code>// Suppose you have a container which is X pixels high and you want to insert a 1-pixel tall // element at each pixel, going vertically var min = Math.min; var max = Math.max; var round = Math.round; function get_color_for_height(startColor, endColor, height, row) { var scale = row/height; var r = startColor[red] + scale*(endColor[red] - startColor[red]); var b = startColor[blue] + scale*(endColor[blue] - startColor[blue]); var g = startColor[green] + scale*(endColor[green] - startColor[green]); return { r: round(min(255, max(0, r))), g: round(min(255, max(0, g))), b: round(min(255, max(0, b))) } } // some psuedo-code using an imaginary framework for(var h = 0; h &lt; height; h++) { var div = new Element('div'); div.height = 1; div.backgroundColor = get_color_for_height(start, end, height, h); container.insert('top', div); } </code></pre>
    singulars
    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. 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