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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@tblackford/blackorwhite

v1.0.2

Published

Detects whether the text on a given colour should be Black or White

Readme

Black Or White

This is a small module that uses a neural network to determine whether the text colour should be black or white. The neural network library that this uses is Brain.js.

Examples

// Include the module
const BlackOrWhite = require('@tblackford/blackorwhite');
// or
import BlackOrWhite from '@tblackford/blackorwhite';

// Make a new instance of it
const bow = new BlackOrWhite();

// This is the background color of the page or component
const backgroundColor = "#663399";

// Find the text color
const textColor = bow.findTextColor(backgroundColor);

// Should print 'white'
console.log(textColor);

You can also add in three arguments to the constructor to customise the returned text colour, what data is used to train, and how the training is done.

const config = {
    white: "eee",
    black: "a4a4a4"
}

const data = [
    {input: { r: 0.0, g: 0.0, b: 0.0 }, output: { white: 1 }},
    {input: { r: 1.0, g: 1.0, b: 1.0 }, output: { black: 1 }},
    {input: { r: 0.8, g: 1, b: 0.4 }, output: { black: 1 }},
    {input: { r: 0.41, g: 0.55, b: 0.77 }, output: { white: 1 }},
    {input: { r: 0.94, g: 0.47, b: 0.39 }, output: { white: 1 }},
    {input: { r: 0.64, g: 0.93, b: 0.44 }, output: { black: 1 }},
    {input: { r: 0.99, g: 0.89, b: 0 }, output: { black: 1 }},
    {input: { r: 0, g: 0.73, b: 0 }, output: { white: 1 }},
]

const nn_config = {
    activation: 'tanh', 
    hiddenLayers: [6],
    learningRate: 0.6 
}

// Make a new instance of it
const bow = new BlackOrWhite(config, data, nn_config);

// This is the background color of the page or component
const backgroundColor = "#663399";

// Find the text color
const textColor = bow.findTextColor(backgroundColor);

// Should print '#eee'
console.log(textColor);

Options

There are two config objects in the constructor as well as an array.

config

Default

// You can customise the white or black colours
// Be careful that they don't stray too far from #000 or #FFF
const config = {    
    white: "white",
    black: "black",
    appendDefaultData: true,
}

data

The data is an array of objects that have two keys: input and output.

  • Input has three keys: r, g, and b which need to be a float between 0 and 1.
  • Output only has one key which is the desired text color which must be 1.

If the appendDefaultData config option is true then the data passed in will be appended to the default data.

Default

const data = [
    {input: { r: 0.0, g: 0.0, b: 0.0 }, output: { white: 1 }},
    {input: { r: 1.0, g: 1.0, b: 1.0 }, output: { black: 1 }},
    {input: { r: 0.8, g: 1, b: 0.4 }, output: { black: 1 }},
    {input: { r: 0.41, g: 0.55, b: 0.77 }, output: { white: 1 }},
    {input: { r: 0.94, g: 0.47, b: 0.39 }, output: { white: 1 }},
    {input: { r: 0.64, g: 0.93, b: 0.44 }, output: { black: 1 }},
    {input: { r: 0.99, g: 0.89, b: 0 }, output: { black: 1 }},
    {input: { r: 0, g: 0.73, b: 0 }, output: { white: 1 }},
]

nn_config

This config object is for the Brain.js neural network constructor.

activation

There are four types of activation functions that Brain.js (and BlackOrWhite) can support:

hiddenLayers

This specifies the number of hidden layers in the network and the number of them.

hiddenLayers: [2, 3]

This would mean that there are two layers with the first having 2 nodes and the second having 3 nodes.

learningRate

This scales with the delta that the network uses and affects the learning rate as well as the outcome.

Default

const nn_config = {
    activation: 'sigmoid', 
    hiddenLayers: [4],
    learningRate: 0.6 
}