Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Converting to HSV to change the brigthness</h2> <p><strong><a href="http://jsfiddle.net/K8cMX/7/" rel="nofollow noreferrer">See the full code example on jsFiddle</a></strong></p> <p>This version uses <code>HSV</code>. I convert the original <code>rgb</code> value to <code>hsv</code> and change the value of <code>v</code> to get a lighter color. I got <a href="https://stackoverflow.com/questions/2348597/why-doesnt-this-javascript-rgb-to-hsl-code-work/2348659#2348659"><code>RgbToHsv</code></a> from Pointy answer, I just added a little fix for gray. And I got <a href="http://www.csgnetwork.com/csgcolorsel4.html" rel="nofollow noreferrer"><code>HsvToRgb</code> on this website</a></p> <p>When the page loads I am getting the <code>rgb</code> converting into <code>hsv</code> changing the <code>v</code> value, convert back to <code>rgb</code> and change the <code>css</code> of the element with the new value.</p> <pre><code>function AppendColor(light) { $(".dark").each(function(i){ // get the RGB from existing elements var color = $(this).css("background-color"); color = color.replace(/[^0-9,]+/g, ""); var red = color.split(",")[0]; var gre = color.split(",")[1]; var blu = color.split(",")[2]; // convert rgb to hsv var hsv = RgbToHsv(red,gre,blu); // convert hsv to rgb modifing the `v` param var rgb = HsvToRgb(hsv.h, hsv.s, light); // creates a new div and set the new background // then appends to the content color = "rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + ")"; $("&lt;div /&gt;") .css("background", color) .attr("title", color) .appendTo($(".light").parent()); $("&lt;span /&gt;").html(" ").appendTo($(".light").parent()) }); $("&lt;br /&gt;").appendTo($(".light").parent()) } // tested values AppendColor(25); AppendColor(50); AppendColor(75); AppendColor(90); AppendColor(95); AppendColor(97); AppendColor(99); AppendColor(100); </code></pre> <p>Result:</p> <p><img src="https://lh5.ggpht.com/_ztQBKXDrT-w/Tbt0iTwICLI/AAAAAAAAA3Y/H6EafLy_2xI/s600/7f88d7bbd2a64378b8313e9925a7db4c.png" alt="rgb to hsv to rgb"></p> <hr> <h2>Increasing color values by highest color</h2> <p><strong><a href="http://jsfiddle.net/K8cMX/2/" rel="nofollow noreferrer">See this example on jsFiddle</a></strong></p> <p>The <code>divs</code> on top represents the dark colors (rgb) <code>#801A00</code>, <code>#00801A</code>, <code>#1A0080</code> and <code>#D2D2D2</code></p> <pre><code>&lt;div id="red" class="dark red"&gt;&lt;/div&gt; &lt;div id="green" class="dark green"&gt;&lt;/div&gt; &lt;div id="blue" class="dark blue"&gt;&lt;/div&gt; &lt;div id="gray" class="dark gray"&gt;&lt;/div&gt; &lt;br /&gt; &lt;div id="lred" class="lred"&gt;&lt;/div&gt; &lt;div id="lgreen" class="lgreen"&gt;&lt;/div&gt; &lt;div id="lblue" class="lblue"&gt;&lt;/div&gt; &lt;div id="lgray" class="lgray"&gt;&lt;/div&gt; </code></pre> <p>The <code>divs</code> on the bottom will get the light color from the dark.</p> <p>When I click a dark div it will retrieve the <code>background-color</code>, split the values and send to the function <code>Lighthen</code>. This function will make the color more light.</p> <pre><code>function Lighthen(red, green, blue) { var max = ([red,green,blue].sort(function(l,r){return r-l}))[0]; var multiplier = max; multiplier = (multiplier / 255) + 1; // if it would still be too dark, make it lighten more if (multiplier &lt; 1.5) multiplier = 1.9; // if it gets to white, move away a bit if ((max * multiplier) &gt; 255) { multiplier = (multiplier / 230) + 1; } var r = Math.round(red * multiplier); var g = Math.round(green * multiplier); var b = Math.round(blue * multiplier); if (r &gt; 255) r = 255; if (g &gt; 255) g = 255; if (b &gt; 255) b = 255; return "rgb(" + r + "," + g + "," + b + ")"; } </code></pre> <p>When the dark div is clicked, the new color is calculated and changed on the correspondent <code>div</code>.</p> <pre><code>$(".dark").click(function(){ var color = $(this).css("background-color"); color = color.replace(/[^0-9,]+/g, ""); var red = color.split(",")[0]; var gre = color.split(",")[1]; var blu = color.split(",")[2]; $("#l" + $(this).attr("id")) .css("background", Lighthen(red, gre, blu)); }); </code></pre> <p>Resulting in</p> <p><a href="http://jsfiddle.net/K8cMX/2/" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mx6Lj.png" alt="click the color to change"></a></p>
    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.
    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