Git Product home page Git Product logo

prisma's Introduction

prisma

A simple string-to-color conversion tool. The string provided generates a unique and consistent color, and an immutable object of the various valid HTML color values are returned.

prisma has no dependencies, and has a tiny footprint of only 1.6kb (minified and gzipped).

Installation

Install via npm:

$ npm i @rapid7/prisma --save

and incorporate into your project:

// ES2015
import prisma from '@rapid7/prisma';

// CommonJS
const prisma = require('@rapid7/prisma').default;

// UMD
const prisma = window.prisma;

Usage

Simply pass a value to the prisma function, and it will return an object with the hashed color information:

const colorizedString = prisma('Hello World!');

/* colorizedString is now the object:
{
  hex: '#7e7be2',
  hsl: 'hsl(242, 64%, 68%)',
  hslArray: [242, 0.64, 0.68],
  hsla: 'hsla(242, 64%, 68%, 1)',
  hslaArray: [242, 0.64, 0.68, 1],
  rgb: 'rgb(126, 123, 226)',
  rgbArray: [126, 123, 226],
  rgba: 'rgba(126, 123, 226, 1)',
  rgbaArray: [126, 123, 226, 1],
  shouldTextBeDark: false,
  shouldTextBeDarkW3C: true
}
*/

Additionally, you can pass an object of options to the call:

const colorizedStringWithOptions = prisma('Hello World!', {
  brightnessThreshold: 135,
  defaultHex: '1d1d1d',
  opacity: 0.5
});

/* colorizedStringWithOptions is now the object:
{
  hex: '#7e7be2',
  hsl: 'hsl(242, 64%, 68%)',
  hslArray: [242, 0.64, 0.68],
  hsla: 'hsla(242, 64%, 68%, 0.5)',
  hslaArray: [242, 0.64, 0.68, 0.5],
  rgb: 'rgb(126, 123, 226)',
  rgbArray: [126, 123, 226],
  rgba: 'rgba(126, 123, 226, 0.5)',
  rgbaArray: [126, 123, 226, 0.5],
  shouldTextBeDark: false,
  shouldTextBeDarkW3C: true
}
*/

Options

brightnessThreshold

Numeric brightness value used as comparator when calculating shouldTextBeDark, where brightness values greater than or equal to the number provided will return true. Valid values are between 0 and 255, with the recommended range between 128 and 145.

  • Default = 130
  • Good = 140
  • Bad = "140"

defaultHex

Hexadecimal code that is the given default for empty strings. Shorthand values or with preceding hash is allowed.

  • Default = 000000
  • Good = ffffff
  • Good = #fff
  • Bad = red

opacity

Numeric opacity value used in rgba and hsla (note that these are the only two colors impacted by opacity).

  • Default = 1
  • Good = 0.5
  • Bad = 50
  • Bad = -0.6

Object properties returned

hex {string}

The hexadecimal color code as a string used in CSS

hsl {string}

The computed values for HSL (hue, saturation, luminance) as a string used in CSS

hslArray {Array<number>}

The computed values for HSL as an array of numbers

hsla {string}

The computed values for HSLA (hue, saturation, luminance, alpha of opacity) as a string used in CSS

hslaArray {Array<number>}

The computed values for HSLA as an array of numbers

rgb {string}

The Computed values for RGB (red, green, blue) as a string used in CSS

rgbArray {Array<number>}

The computed values for RGB as an array of numbers

rgba {string}

The Computed values for RGBA (red, green, blue, alpha of opacity) as a string used in CSS

rgbaArray {Array<number>}

The computed values for RGBA as an array of numbers

shouldTextBeDark {boolean}

Boolean value denoting if using this color as a background-color on an element, should the foreground text color be dark. This is based on an opinionated gamma value threshold.

shouldTextBeDarkW3C {boolean}

Boolean value denoting if using this color as a background-color on an element, should the foreground text color be dark. This is based on the W3C Relative Luminance Definition that is defined in the W3 spec.

Additional functions

prisma.shouldTextBeDark(hexColor: string[, brightnessThreshold: number]) returns {boolean}

Pass a hexadecimal color to it (with or without leading hash), and it returns whether the foreground text should be dark or not based on brightness level.

prisma.shouldTextBeDark('#000'); // false
prisma.shouldTextBeDark('ffffff'); // true

prisma.shouldTextBeDarkW3C(hexColor: string[, brightnessThreshold: number]) returns {boolean}

Pass a hexadecimal color to it (with or without leading hash), and it returns whether the foreground text should be dark or not based on the W3C standard.

prisma.shouldTextBeDarkW3C('#000'); // false
prisma.shouldTextBeDarkW3C('ffffff'); // true

How does it work?

Internally the string is hashed using a simple bitwise operation, and the resultant integer is converted into a hexadecimal code that the other values are built from. Because the hash is a bitwise operation based on charCodeAt, the consistency of the hash is guaranteed, and therefore the resultant colors are as well.

Development

Clone the git repository, and run npm install. From there, you can run any of the following npm scripts:

  • build = builds the library with NODE_ENV=development and with source maps, outputting to the dist folder
  • build:minified = builds the library with NODE_ENV=production and minified, outputting tot the dist folder
  • dev = runs the playground React application to see prisma in action. Have fun playing!
  • lint = runs ESLint against the files in src
  • prepublish = runs lint, transpile, build, and build-minified
  • transpile = runs babel to transpile the files in src to an ES5-compliant version in the lib folder

prisma's People

Contributors

erran avatar planttheidea avatar tquetano-r7 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.