npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

materialcolorize

v1.1.7

Published

A library to find the closest colors on the Google material design color palette

Downloads

11

Readme

MaterialColorizeJS

A library to convert a color to its closest on Google's Material Design Color Palette

Try the live demonstration!

There is a Java version available too!

Installing

The library is available as a node module. Use the --save flag to save this as a dependency in your package.json.

npm install materialcolorize

Once you have it installed, import the desired functions.

ES6:

    import { approximateColor, getColorFamily } from 'materialcolorize'
    // or
    import mc from 'materialcolorize'

ES5:

    var approximateColor = require('materialcolorize').approximateColor;
    // or
    var mc = require('materialcolorize');

If you plan on using this on the client side, I recommend using Browserify or Webpack to be able to require on the client side.

Usage

approximateColor(color)

Pass in a color as a hexstring or hexadecimal number and approximateColor will return the closest material color as a string.

Note: This function does not take 3 digit hex values yet, but that feature will be added in a later release.

    // returns '4CAF50'
    approximateColor('#4AAA58');

    // returns 'FF5722'
    approximateColor('F7642A');

    // returns '80DEEA'
    approximateColor(0x78E1F6);

=============

getColorFamily(color)

Pass in a color as a hexstring or hexadecimal number and getColorFamily will return the family palette of the input color. The keys represent each color's weight.

    let greenPalette = getColorFamily('ADCF83');
    // greenPalette is:
    {
      '50': 'F1F8E9',
      '100': 'DCEDC8',
      '200': 'C5E1A5',
      '300': 'AED581',
      '400': '9CCC65',
      '500': '8BC34A',
      '600': '7CB342',
      '700': '689F38',
      '800': '558B2F',
      '900': '33691E',
      'A100': 'CCFF90',
      'A200': 'B2FF59',
      'A400': '76FF03',
      'A700': '64DD17'
    }

=============

colorDistance(color1, color2)

Calculates the 'distance' between two colors by implementing this formula. approximateColor() uses this to and returns the material color with the minimum distance.

There are limited reasons to use this function alone, but one useful case is deciding whether to use black or white text on a colored background (this is an example that is used for the demo page to assign the color of the text over the family palette colors):

  let displayTextColor = (color) => {
    // If the input color (each color in the family palette) is 'closer' to black than it is to white,
    // set the overlying text color to white
    if(mc.colorDistance(color, '000000') < mc.colorDistance(color, 'FFFFFF')) {
      return 'FFFFFF';
    } else {
      return '000000';
    }
  }

Contributors

Made by Varun Munjeti and Hyunbin Park. Special thanks to Arjun Sarode for helping us get ES6 set up and with React for the demo page!