Sha256: 4fe593dfce35ae113e9f6c18faa231afca0068a34940321cf4b81e8fadc34e85

Contents?: true

Size: 1.14 KB

Versions: 73

Compression:

Stored size: 1.14 KB

Contents

function isDark(color) {
  // Variables for red, green, blue values
  let r;
  let g;
  let b;
  let hsp;

  // If hex --> Convert it to RGB: http://gist.github.com/983661
  color = +`0x${color.slice(1).replace(color.length < 5 && /./g, "$&$&")}`;

  r = color >> 16;
  g = (color >> 8) & 255;
  b = color & 255;

  // HSP equation from http://alienryderflex.com/hsp.html
  hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b));

  // Using the HSP value, determine whether the color is light or dark
  if (hsp > 127.5) {
    return false; // light
  }

  return true; // dark
}

function isLight(color) {
  return !isDark(color);
}

function isValidHex(color) {
  if (!color || typeof color !== "string") return false;

  if (color.substring(0, 1) === "#") color = color.substring(1);

  switch (color.length) {
    case 3:
      return /^[0-9A-F]{3}$/i.test(color);
    case 6:
      return /^[0-9A-F]{6}$/i.test(color);
    case 8:
      return /^[0-9A-F]{8}$/i.test(color);
    default:
      return false;
  }
}

function formatHex(str) {
  return `#${str.toUpperCase().replace("#", "")}`;
}

export { isLight, isDark, isValidHex, formatHex };

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
coveragebook_components-0.19.8 app/assets/js/helpers/color.js
coveragebook_components-0.19.7 app/assets/js/helpers/color.js
coveragebook_components-0.19.6 app/assets/js/helpers/color.js
coveragebook_components-0.19.5 app/assets/js/helpers/color.js
coveragebook_components-0.19.4 app/assets/js/helpers/color.js
coveragebook_components-0.19.3 app/assets/js/helpers/color.js
coveragebook_components-0.19.2 app/assets/js/helpers/color.js
coveragebook_components-0.19.1 app/assets/js/helpers/color.js
coveragebook_components-0.19.0 app/assets/js/helpers/color.js
coveragebook_components-0.18.8 app/assets/js/helpers/color.js
coveragebook_components-0.18.7 app/assets/js/helpers/color.js
coveragebook_components-0.18.0 app/assets/js/helpers/color.js
coveragebook_components-0.17.7 app/assets/js/helpers/color.js
coveragebook_components-0.17.6 app/assets/js/helpers/color.js
coveragebook_components-0.17.5 app/assets/js/helpers/color.js
coveragebook_components-0.17.4 app/assets/js/helpers/color.js
coveragebook_components-0.17.3 app/assets/js/helpers/color.js
coveragebook_components-0.17.2 app/assets/js/helpers/color.js
coveragebook_components-0.17.1 app/assets/js/helpers/color.js
coveragebook_components-0.17.0 app/assets/js/helpers/color.js