123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="extern">
  <h2>Analizador de contraste de colores</h2>
  <p class="fx"><span>
  <label>Color de fondo: </label>
  <input type="text" class="c1" name="background" value="#152B39">
  </span>
    <span>
  <label>Color del texto: </label>
  <input type="text" class="c2" name="text" value="#abcdef">
</span></p>


  <div class="el">
    <h3 class="large"></h3>
    <p class="normal"></p>
    <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia
      voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi
      tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui
      in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
    </p>
    <div class="output">8.8</div>
  </div>
  <h2>La normativa</h2>
<p>
  El nivel AA: requiere un ratio de contraste de 4.5:1 para texto normal <em>( Esto equivale a un aprobado justo )</em></p>
<p>El nivel AAA: requiere un ratio de contraste de 7:1 para texto normal. <em>( Esto equivale a un aprobado con notable )</em></p>
<p>Definimos "texto grande" como texto de mínimum 14pt ( 18px ) y en negrita,       o de mínimum 18pt ( 24px ).</p>
  <p><em><a target="_blank" href="https://www.w3.org/TR/WCAG/#visual-audio-contrast">Web Content Accessibility Guidelines (WCAG) 2.0 <img class="data_image" width="12" height="12" alt="star" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAYAAABWdVznAAAA/klEQVQoU3WQMZIBYRCFX48Jtgi4gZHZmkhAihsQErnBrhvsDRxhCIwQJyA2gRGokuEGEmqr7GrdGPUPNRP98/73/a9fE4zv0w++LeBHpKyp6/mfUN+0ynN6vSiOlk6K//qiVy9AlwlhijFLBJzxMpc+nWdEWK3blc7jge0TcP2AX1MAHqjZHS46APVAyMUAjZaLncXsgXj6NBN5zNhLWj4G6I91QU0KfR3TdiFzOjcg5nsHeyKdQvE0bqV1pIh2/UVf5qsSyDE3o9vT8m+AmhQCY64J0UOq6zJ2zdIhlmCWN5NN/Qa8b+mumAmRh4qjoJYE/H7YoY4RS0gyJ+lXBviSDaxQWVkAAAAASUVORK5CYII=" /></a></em></p> 
</div>
            
          
!
            
              html {
  font-size: 100%;
  margin: 0;
  padding: 0;
  font-family: Arial, Helvetica, sans-serif;
}

h2 {
  color: #152B39;
  margin: 1em 0;
  border-bottom: 1px solid #d9d9d9;
  /*font-family:Georgia, "Times New Roman", Times, serif;*/
}

em {
  font-family: serif;
}

a {
  text-decoration: none;
  color: #2798ba;
}

a:hover {
  text-decoration: underline;
}

.extern {
  max-width: 55rem;
  padding: 0 1rem;
  margin: 4rem auto;
}

.el {
  display: block;
  border: 1px solid #d9d9d9;
  max-width: 50rem;
  min-width:20rem;
  padding: 2em;
  margin-right: 1.5em;
  line-height: 150%;
  position: relative;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

p.fx {
  display: flex;
}

input {
  margin-bottom: 2em;
  padding: .5em;
  background-color: white;
  border: 1px solid #cccccc;
  -webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
}

input:focus {
  -webkit-box-shadow: 0 0 5px #999999;
  -moz-box-shadow: 0 0 5px #999999;
  box-shadow: 0 0 5px #999999;
  border-color: #999999;
  outline: none;
}

label {
  margin-right: 1em;
}

p.fx span {
  flex: 1 0 50%;
}

.large {
  font-size: 1.8em;
  font-weight: bold
}

.normal {
  font-size: 1em;
}

.output {
  font-size: 200%;
  width: 3.5em;
  height: 3.5em;
  text-align: center;
  line-height: 350%;
  border-radius: 50%;
  background-color: #6ab150;
  color: white;
  position: absolute;
  top: -.7em;
  left: calc(100% - 2.8em);
  box-shadow: -.1em -.1em .5em rgba(0, 0, 0, .5) inset;
}
            
          
!
            
              function cz_validateHex(hex) {
  return /(^#?[0-9A-F]{6}$)|(^#?[0-9A-F]{3}$)/i.test(hex);
}

function cz_validateHsl(hsl) {
  return /(^hsl[(]\s*0*(?:[12]?\d{1,2}|3(?:[0-5]\d|60))\s*(?:\s*,\s*0*(?:\d\d?(?:\.\d+)?\s*%|\.\d+\s*%|100(?:\.0*)?\s*%)){2}\s*[)]$)/i.test(hsl);
}

function cz_validateRgb(rgb) {
  return /(^(?:(?:^|,\s*)([01]?\d\d?|2[0-4]\d|25[0-5])){3}$)/i.test(rgb);
}

function helper_hsl2hex(v1, v2, vh) { //to be used width hsl2hex();

  if (vh < 0) {
    vh += 1;
  };

  if (vh > 1) {
    vh -= 1;
  };

  if ((6 * vh) < 1) {
    return (v1 + (v2 - v1) * 6 * vh);
  };

  if ((2 * vh) < 1) {
    return (v2);
  };

  if ((3 * vh) < 2) {
    return (v1 + (v2 - v1) * ((2 / 3 - vh) * 6));
  };

  return v1;

}

function cz_getHslRy(hsl) {
  hsl = hsl.slice(4);
  hsl = hsl.split(")");
  hslRy = hsl[0].split(",");
  return hslRy.map(function(c) {
    return c.replace("%", "");
  })
}

function cz_hsl2rgb(hsl) { //hsl($h,$s%,$l%);
  var hslRy = getHslRy(hsl);
  h = hslRy[0] / 360;
  s = hslRy[1] / 100;
  l = hslRy[2] / 100;

  if (s == 0) //gray
  {
    r = l * 255;
    g = l * 255;
    b = l * 255;
  } else {
    if (l < 0.5) //dark
    {
      v2 = l * (1 + s);
    } else //light
    {
      v2 = (l + s) - (s * l);
    };

    v1 = 2 * l - v2;

    r = 255 * helper_hsl2hex(v1, v2, h + (1 / 3));
    g = 255 * helper_hsl2hex(v1, v2, h);
    b = 255 * helper_hsl2hex(v1, v2, h - (1 / 3));

  }

  return [r, g, b];
  /*OK*/
} //console.log(hsl2rgb("hsl(45,100%,50%)"))

function cz_hex2rgb(hexColor) {

  hexColor = hexColor.replace("#", "");
  if (hexColor.length == 6) {

    var redhex = hexColor.substr(0, 2);
    var greenhex = hexColor.substr(2, 2);
    var bluehex = hexColor.substr(4, 2);
  } else if (hexColor.length == 3) {
    var redhex = hexColor.substr(0, 1) + hexColor.substr(0, 1);
    var greenhex = hexColor.substr(1, 1) + hexColor.substr(1, 1);
    var bluehex = hexColor.substr(2, 1) + hexColor.substr(2, 1);
  }
  console.log(redhex);

  var rgbRy = [];

  rgbRy[0] = parseInt(redhex, 16);

  rgbRy[1] = parseInt(greenhex, 16);

  rgbRy[2] = parseInt(bluehex, 16);

  return rgbRy; /*OK*/
} //console.log(hex2rgb("ffbf00"))

function cz_rgb2rgb(rgb) {
  rgb = rgb.slice(4);
  rgb = rgb.split(")");
  rgbRy = rgb[0].split(",");
  return rgbRy.map(function(c) {
    return parseInt(c);
  });
} //console.log(rgb2rgb("rgb(255,191,0)"))

function cz_whatColor(str) {
  if (cz_validateHsl(str)) {
    return cz_hsl2rgb(str);
  } else if (cz_validateRgb(str)) {
    return cz_rgb2rgb(str);
  } else if (cz_validateHex(str)) {
    return cz_hex2rgb(str);
  } else {
    return ([255, 255, 255]);
  }
}

/* LEA VEROU https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html*/
function cz_getRelativeLuminance(rgb) { //relative luminance
  var rgb = rgb.map(function(c) {
    c /= 255;
    return c < .03928 ? c / 12.92 : Math.pow((c + .055) / 1.055, 2.4);

  });

  return (21.26 * rgb[0] + 71.52 * rgb[1] + 7.22 * rgb[2]) / 100
}

function cz_colorContrast(c1, c2) {
  var l1 = cz_getRelativeLuminance(c1);
  var l2 = cz_getRelativeLuminance(c2);
  var ret = (l1 + .05) / (l2 + .05);
  // 0.05 for not dividing with 0
  return ret < 1 ? 1 / ret : ret;
}

var normal = document.querySelector(".normal");
var large = document.querySelector(".large");
var el = document.querySelector(".el");
var output = document.querySelector(".output");
var inputBackground = document.querySelector(".c1")
var inputText = document.querySelector(".c2")
var backgroundColor = inputBackground.value;
var textColor = inputText.value;
var c1 = cz_whatColor(backgroundColor);
var c2 = cz_whatColor(textColor);

function cz_getContrast() {
  backgroundColor = inputBackground.value;
  textColor = inputText.value;
  c1 = cz_whatColor(backgroundColor);
  c2 = cz_whatColor(textColor);
  el.style.backgroundColor = "rgb(" + c1[0] + "," + c1[1] + "," + c1[2] + ")";
  el.style.color = "rgb(" + c2[0] + "," + c2[1] + "," + c2[2] + ")";
  large.innerHTML = "Para texto grande: ";
  normal.innerHTML = "Para texto normal: ";
  var cc = cz_colorContrast(c1, c2);

  output.innerHTML = parseFloat(cc).toFixed(1);
  if (cc >= 7) {
    large.innerHTML += " aprobado AAA";
    normal.innerHTML += " aprobado AAA";
    output.style.backgroundColor = "#6ab150";
    output.style.color = "#fff";
  } else if (cc >= 4.5 && cc < 7) {
    large.innerHTML += " aprobado AAA";
    normal.innerHTML += " aprobado AA";
    output.style.backgroundColor = "#ffff00";
    output.style.color = "#222";
  } else if (cc >= 3 && cc < 4.5) {
    large.innerHTML += " aprobado AA";
    normal.innerHTML += " falla";
    output.style.backgroundColor = "#ff7b00";
    output.style.color = "#fff";
  } else {
    large.innerHTML += " falla";
    normal.innerHTML += " falla";
    output.style.backgroundColor = "#dd0000";
    output.style.color = "#fff";
  }
}

window.addEventListener("load", cz_getContrast, false);
inputBackground.addEventListener("input", cz_getContrast, false);
inputText.addEventListener("input", cz_getContrast, false);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console