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.6.4 app/assets/js/helpers/color.js
coveragebook_components-0.6.3 app/assets/js/helpers/color.js
coveragebook_components-0.6.2 app/assets/js/helpers/color.js
coveragebook_components-0.6.1 app/assets/js/helpers/color.js
coveragebook_components-0.6.0 app/assets/js/helpers/color.js
coveragebook_components-0.5.7 app/assets/js/helpers/color.js
coveragebook_components-0.5.6 app/assets/js/helpers/color.js
coveragebook_components-0.5.5 app/assets/js/helpers/color.js
coveragebook_components-0.5.4 app/assets/js/helpers/color.js
coveragebook_components-0.5.3 app/assets/js/helpers/color.js
coveragebook_components-0.5.2 app/assets/js/helpers/color.js
coveragebook_components-0.5.1 app/assets/js/helpers/color.js
coveragebook_components-0.5.0 app/assets/js/helpers/color.js