Skip to content

Instantly share code, notes, and snippets.

@ExFlo
Forked from jedfoster/.gitignore
Created April 14, 2020 14:20
Show Gist options
  • Select an option

  • Save ExFlo/f1e0620a09f437af6bde400e879e98ac to your computer and use it in GitHub Desktop.

Select an option

Save ExFlo/f1e0620a09f437af6bde400e879e98ac to your computer and use it in GitHub Desktop.

Revisions

  1. @jedfoster jedfoster revised this gist Dec 13, 2013. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions roughdraft-1.md
    Original file line number Diff line number Diff line change
    @@ -37,11 +37,12 @@ Because it only accepts 6-digit hex values you may need to convert your color fr

    ```javascript
    var rgb2hex = function(rgb) {
    // A very ugly regex that parses a string such as 'rgb(191, 0, 46)' and produces an array
    rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d\.]+))?\)$/);
    function hex(x) {
    return ("0" + parseInt(x).toString(16)).slice(-2);
    }
    return (hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3])).toUpperCase();

    function hex(x) { return ("0" + parseInt(x).toString(16)).slice(-2); } // another way to convert a decimal to hex

    return (hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3])).toUpperCase(); // concatenate the pairs and return them upper cased
    };
    ```

  2. @jedfoster jedfoster revised this gist Dec 13, 2013. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions roughdraft-1.md
    Original file line number Diff line number Diff line change
    @@ -4,25 +4,26 @@ Here's my JavaScript version of Sass' `mix()` function.

    ```javascript
    var mix = function(color_1, color_2, weight) {
    function d2h(d) { return d.toString(16); }
    function h2d(h) { return parseInt(h, 16); }
    function d2h(d) { return d.toString(16); } // convert a decimal value to hex
    function h2d(h) { return parseInt(h, 16); } // convert a hex value to decimal

    weight = (typeof(weight) !== 'undefined') ? weight : 50;
    weight = (typeof(weight) !== 'undefined') ? weight : 50; // set the weight to 50%, if that argument is omitted

    var color = "#";

    for(var i = 0; i <= 5; i += 2) {
    var v1 = h2d(color_1.substr(i, 2)),
    for(var i = 0; i <= 5; i += 2) { // loop through each of the 3 hex pairs—red, green, and blue
    var v1 = h2d(color_1.substr(i, 2)), // extract the current pairs
    v2 = h2d(color_2.substr(i, 2)),
    val = d2h(Math.floor(v2 + (v1 - v2) * (weight / 100.0)));

    // combine the current pairs from each source color, according to the specified weight
    val = d2h(Math.floor(v2 + (v1 - v2) * (weight / 100.0)));

    while(val.length < 2){
    val = '0' + val;
    }
    color += val;
    while(val.length < 2) { val = '0' + val; } // prepend a '0' if val results in a single digit

    color += val; // concatenate val to our new color string
    }

    return color;
    return color; // PROFIT!
    };
    ```

  3. @jedfoster jedfoster revised this gist Dec 13, 2013. No changes.
  4. @jedfoster jedfoster revised this gist Dec 13, 2013. 1 changed file with 1 addition and 8 deletions.
    9 changes: 1 addition & 8 deletions roughdraft-1.md
    Original file line number Diff line number Diff line change
    @@ -53,11 +53,4 @@ var color2 = window.getComputedStyle(element2).backgroundColor; // '0000bb'
    var mixed = mix(rgb2hex(color1), rgb2hex(color2), 75); // returns #bf002e
    ```






    http://sassmeister.com/gist/7939513


    Play with the Sass version over on [SassMeister.com](http://sassmeister.com/gist/7939513)
  5. @jedfoster jedfoster revised this gist Dec 13, 2013. 2 changed files with 50 additions and 24 deletions.
    21 changes: 0 additions & 21 deletions color.js
    Original file line number Diff line number Diff line change
    @@ -1,21 +0,0 @@
    var mix = function(color_1, color_2, weight) {
    function d2h(d) { return d.toString(16); }
    function h2d(h) { return parseInt(h, 16); }

    weight = (typeof(weight) !== 'undefined') ? weight : 50;

    var color = "#";

    for(var i = 0; i <= 5; i += 2) {
    var v1 = h2d(color_1.substr(i, 2)),
    v2 = h2d(color_2.substr(i, 2)),
    val = d2h(Math.floor(v2 + (v1 - v2) * (weight / 100.0)));

    while(val.length < 2){
    val = '0' + val;
    }
    color += val;
    }

    return color;
    };
    53 changes: 50 additions & 3 deletions roughdraft-1.md
    Original file line number Diff line number Diff line change
    @@ -2,15 +2,62 @@ I recently found myself needing to combine two colors. [Sass has a great functio

    Here's my JavaScript version of Sass' `mix()` function.

    <gist>color.js</gist>
    ```javascript
    var mix = function(color_1, color_2, weight) {
    function d2h(d) { return d.toString(16); }
    function h2d(h) { return parseInt(h, 16); }

    weight = (typeof(weight) !== 'undefined') ? weight : 50;

    var color = "#";

    for(var i = 0; i <= 5; i += 2) {
    var v1 = h2d(color_1.substr(i, 2)),
    v2 = h2d(color_2.substr(i, 2)),
    val = d2h(Math.floor(v2 + (v1 - v2) * (weight / 100.0)));

    while(val.length < 2){
    val = '0' + val;
    }
    color += val;
    }

    return color;
    };
    ```

    Unlike the Sass version, my JavaScript version only accepts 6-digit hex values for the colors (`#ff0000` works, but `#f00` does not.) Like the Sass version, the `weight` argument is an integer value between 0 and 100.

    ```javascript
    var mixed = mix('ff0000', '0000bb', 75); // #bf002e
    var mixed = mix('ff0000', '0000bb', 75); // returns #bf002e
    ```

    Because it only accepts 6-digit hex values you may need to convert your color from RGB, particularly if you obtained it using something like `window.getComputedStyle(div).backgroundColor;`, as I did. In that case, this function has you covered:

    ```javascript
    var rgb2hex = function(rgb) {
    rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d\.]+))?\)$/);
    function hex(x) {
    return ("0" + parseInt(x).toString(16)).slice(-2);
    }
    return (hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3])).toUpperCase();
    };
    ```

    Use it like so:

    ```javascript
    var color1 = window.getComputedStyle(element1).backgroundColor; // 'ff0000'
    var color2 = window.getComputedStyle(element2).backgroundColor; // '0000bb'

    var mixed = mix(rgb2hex(color1), rgb2hex(color2), 75); // returns #bf002e
    ```






    http://sassmeister.com/gist/7939894
    http://sassmeister.com/gist/7939513


  6. @jedfoster jedfoster revised this gist Dec 13, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion SassMeister-rendered.html
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    <div></div>
  7. @jedfoster jedfoster revised this gist Dec 13, 2013. 5 changed files with 35 additions and 6 deletions.
    10 changes: 10 additions & 0 deletions SassMeister-input.scss
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    // ----
    // Sass (v3.3.0.rc.1)
    // Compass (v0.13.alpha.10)
    // ----

    body {
    width: 100%;
    height: 10em;
    background: mix(#ff0000, #0000bb, 75);
    }
    5 changes: 5 additions & 0 deletions SassMeister-output.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    body {
    width: 100%;
    height: 10em;
    background: #bf002e;
    }
    1 change: 1 addition & 0 deletions SassMeister-rendered.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <div></div>
    8 changes: 4 additions & 4 deletions color.js
    Original file line number Diff line number Diff line change
    @@ -6,16 +6,16 @@ var mix = function(color_1, color_2, weight) {

    var color = "#";

    for(var i=0; i <= 5; i+=2) {
    for(var i = 0; i <= 5; i += 2) {
    var v1 = h2d(color_1.substr(i, 2)),
    v2 = h2d(color_2.substr(i, 2)),
    val = d2h(Math.floor(v2 + (v1 - v2) * (weight/100.0) ));
    val = d2h(Math.floor(v2 + (v1 - v2) * (weight / 100.0)));

    while(val.length < 2){
    val = "0"+val;
    val = '0' + val;
    }
    color += val;
    }

    return color;
    }
    };
    17 changes: 15 additions & 2 deletions roughdraft-1.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,16 @@
    This is a JavaScript version of Sass' `mix()` function.
    I recently found myself needing to combine two colors. [Sass has a great function for doing this](http://sass-lang.com/documentation/Sass/Script/Functions.html#mix-instance_method), even allowing you to control the percentage of each color, but in my case the two colors needed to change every few seconds, so Sass was out of the question. Enter JavaScript.

    Here's my JavaScript version of Sass' `mix()` function.

    <gist>color.js</gist>

    Unlike the Sass version, my JavaScript version only accepts 6-digit hex values for the colors (`#ff0000` works, but `#f00` does not.) Like the Sass version, the `weight` argument is an integer value between 0 and 100.

    ```javascript
    var mixed = mix('ff0000', '0000bb', 75); // #bf002e
    ```


    http://sassmeister.com/gist/7939894


    <gist>color.js</gist>
  8. @jedfoster jedfoster revised this gist Dec 13, 2013. 1 changed file with 3 additions and 12 deletions.
    15 changes: 3 additions & 12 deletions color.js
    Original file line number Diff line number Diff line change
    @@ -2,23 +2,14 @@ var mix = function(color_1, color_2, weight) {
    function d2h(d) { return d.toString(16); }
    function h2d(h) { return parseInt(h, 16); }

    weight = weight || 50;

    if(weight == 0) {
    return color_1;
    }

    if(weight == 100) {
    return color_2;
    }
    weight = (typeof(weight) !== 'undefined') ? weight : 50;

    var color = "#";

    for(var i=0; i <= 5; i+=2) {
    var v1 = h2d(color_1.substr(i, 2)),
    v2 = h2d(color_2.substr(i, 2));

    var val = d2h(Math.floor(v1 + (v2 - v1) * (weight/100.0) ));
    v2 = h2d(color_2.substr(i, 2)),
    val = d2h(Math.floor(v2 + (v1 - v2) * (weight/100.0) ));

    while(val.length < 2){
    val = "0"+val;
  9. @jedfoster jedfoster revised this gist Dec 13, 2013. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions color.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,3 @@
    var rgb2hex = function(rgb) {
    rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d\.]+))?\)$/);
    function hex(x) {
    return ("0" + parseInt(x).toString(16)).slice(-2);
    }
    return (hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3])).toUpperCase();
    }

    var mix = function(color_1, color_2, weight) {
    function d2h(d) { return d.toString(16); }
    function h2d(h) { return parseInt(h, 16); }
  10. @jedfoster jedfoster revised this gist Dec 13, 2013. 2 changed files with 3 additions and 1 deletion.
    File renamed without changes.
    4 changes: 3 additions & 1 deletion roughdraft-1.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,3 @@
    This is a JavaScript version of Sass' `mix()` function.
    This is a JavaScript version of Sass' `mix()` function.

    <gist>color.js</gist>
  11. @jedfoster jedfoster revised this gist Dec 13, 2013. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions roughdraft-2.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    var rgb2hex = function(rgb) {
    rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d\.]+))?\)$/);
    function hex(x) {
    return ("0" + parseInt(x).toString(16)).slice(-2);
    }
    return (hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3])).toUpperCase();
    }

    var mix = function(color_1, color_2, weight) {
    function d2h(d) { return d.toString(16); }
    function h2d(h) { return parseInt(h, 16); }

    weight = weight || 50;

    if(weight == 0) {
    return color_1;
    }

    if(weight == 100) {
    return color_2;
    }

    var color = "#";

    for(var i=0; i <= 5; i+=2) {
    var v1 = h2d(color_1.substr(i, 2)),
    v2 = h2d(color_2.substr(i, 2));

    var val = d2h(Math.floor(v1 + (v2 - v1) * (weight/100.0) ));

    while(val.length < 2){
    val = "0"+val;
    }
    color += val;
    }

    return color;
    }
  12. @jedfoster jedfoster created this gist Dec 13, 2013.
    1 change: 1 addition & 0 deletions roughdraft-1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    This is a JavaScript version of Sass' `mix()` function.