Git Product home page Git Product logo

invert-color's Introduction

invert-color

build-status Coverage Status Known Vulnerabilities npm release downloads license typescript

© 2021, Onur Yıldırım (@onury). MIT License.

Generates inverted (opposite) version of the given color. (<1KB)

This passes a long test suite of Adobe Photoshop CC inverted colors...
Generating exactly the same results with it.

Invert Animation

Usage

npm i invert-color

// Node, CommonJS
const invert = require('invert-color');
// ES2015, JSNext
import invert from 'invert-color';
// TypeScript
import invert, { RGB, RgbArray, HexColor, BlackWhite } from 'invert-color';

For UMD in browser, use lib/invert.min.js. See other exports.

invert(color[, bw])

  • color : String|Array|Object
    Color in HEX string, RGB array or RGB object to be inverted.
  • bw : Boolean|Object
    Optional. A boolean value indicating whether the output should be amplified to black (#000000) or white (#ffffff), according to the luminance of the original color. You can set custom black/white values (and/or luminance threshold) by passing an object.
invert('#000')              // —> #ffffff
invert('#282b35')           // —> #d7d4ca

// input color as RGB array or object
invert([69, 191, 189])              // —> #ba4042
invert({ r: 249, g: 119, b: 121 })  // —> #068886

// amplify to black or white
invert('#282b35', true)     // —> #ffffff

// amplify to custom black or white color
invert('#282b35', { black: '#3a3a3a', white: '#fafafa' })     // —> #fafafa

// amplify with custom luminance threshold (default is invert.defaultThreshold = ~0.179)
invert('#282b35', { black: '#3a3a3a', white: '#fafafa', threshold: 0.01 })     // —> #3a3a3a

invert.asRGB(color[, bw])

Invert and output result as RGB object.

invert.asRGB('#fff')          // —> { r: 0, g: 0, b: 0 }

invert.asRgbArray(color[, bw])

Invert and output result as RGB array.

invert.asRgbArray('#000')      // —> [255, 255, 255]

bw option

This is useful in case, you need to create contrast (i.e. background vs foreground, for better readability). The animation at the top is a demonstration.

Contributing

Clone original project:

git clone https://github.com/onury/invert-color.git

Install (dev) dependencies:

npm install

Add tests into test/unit.test.ts and run:

npm run cover

Travis build should pass, coverage should not degrade.

Change-Log

v2.0.0 (2018-11-09)

  • Breaking: In order to be consistent; now using default export only. Added ESM, UMD, CommonJS bundles (with rollup). See Usage section.
  • In addition to main, package.json now also defines module, jsnext:main and browser.
  • Added threshold: number to BlackWhite options (interface). Fixes #16.
  • Added invert.defaultThreshold constant.

v1.5.0 (2018-08-22)

  • Re-written in TypeScript.
  • Added .asRGB() - alias of .asRgbObject().

v1.2.3 (2018-04-05)

v1.2.2 (2017-12-07)

  • Fixed an issue with UMD output. Fixes #7.
  • (Dev) Adapted webpack for UMD.

v1.2.0 (2017-11-24)

  • Added UMD support. (PR #6 by @criography - revised for latest Babel.)
  • (Dev) Migrated tests to Jest (dropped Jasmine).

v1.1.0 (2017-11-07)

  • Added ability to customize black/white color values. (PR #3 by @BrainCrumbz)
  • Fixed typo. (PR #1 by @villfa)
  • Minor revisions.

v1.0.0 (2017-08-22)

  • Initial version.

License

MIT.

invert-color's People

Contributors

caydenberg avatar giuseppepiscopo avatar onury avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

invert-color's Issues

Request for better error message(s)

If you pass an invalid color into invert you get

Uncaught TypeError: Cannot read property 'r' of null

which is hard to interpret, and doesn't give you much clue that the error is occurring in this package.

I'd be happy to contribute a PR if you're likely to merge it.

Is it possible to customize the contrast threshold?

In bootstrap 4 we have a variable called $yiq-contrasted-threshold: were has a value of 150 by default and this variable ajusts the threshold to decide if the color should be black or white in components. Is there something similar to this?

I thought something like this:

invert('#d9534f', { threshold: 155 }); // #ffffff

And congrats for this awesome project! Thank you very much!

Fails for #7f7f7f

If you pass in #7f7f7f you get back #7f7f7f if bw is false.

IMO it's best to always use bw.

TS2345

import invert from 'invert-color';

let _channels = [0, 0, 0];

let _new = invert.asRGB(_channels); //error

expected: This should work, as per:

https://github.com/onury/invert-color/blob/cd183934b67e4536e4e3f8a618067fb861fd63e2/lib/invert.d.ts

/**
 *  RGB list (array) type with red, green and blue components.
 */
export declare type RgbArray = [number, number, number];
...
export declare type Color = RGB | RgbArray | HexColor;

observed:

TS2345: Argument of type 'number[]' is not assignable to parameter of type 'Color'.
  Type 'number[]' is not assignable to type 'RgbArray'.
    Target requires 3 element(s) but source may have fewer.

Address

Hey, at what address I can contact you directly?

So does it invert brightness or only hue?

Your lib looks nice, but actually I am still not sure whether it inverts in the way I need it.

So here is the back story: In short, I need it for my add-on, which wants to get the best contrasting color for a given color, as it can otherwise not be scanned by QR code scanners.
As such the first implementation turned out to have a little issue being that it inverts the brightness, too.
I am still not really convinced whether this is good for my aim or not, but well...
Seeing this only inverting the hue seems to be a little more complicated, so I stumbled upon your lib (here of course).

So back to my questions:

  • So this is inverting only the hue, is not it?
  • Maybe, you may add the feature to also invert the brightness, too? Or is there no actual use case for that?
  • And may I be better off with using this lib and only the hue?

Generally my problem/use case of course would need a very clever way to get the "best" contrast somehow. If you have any tips/ideas, feel free to comment.

Chore: clean up published package directory

Looking at package contents when installed from client code, there are more files than needed. That makes package larger and less clear to reason about.

It'd be nice to achieve a package directory listing like:

index.js
LICENSE
package.json
README.md

If you're ok with that, we already have a working PR.

Apart from changes from that PR, one important bit is that the package file to be published should be generated by running npm pack (instead of zipping the whole directory content, or some similar procedure).

What's your take about it? Thanks

Feature proposal: pass dark/light color codes for bw option

Hi there

currently in a project we're using a combination of color-convert plus custom code to decide if a color is "bright" or dark, so to apply an opposite custom dark/light text color.
Our dark/light text colors are not pure #000/#FFF, but somehow closer.

Would you consider a different API like e.g. the following, here expressed as TypeScript just for clarity:

invert(colorCode: string, bw: boolean | BwColorCodes);

interface BwColorCodes {
  dark: string;
  light: string;
}

So all in all you could invoke invertColor(colorCode, true) to get current B/W behavior, or you could also call:

invertColor(colorCode, {
  dark: myCustomDarkColor,
  light: myCustomLightColor,
});

In case, what would be the other API that needs change? Would you consider a PR targeting this?

Thanks

Missing typings

Missing typings file or deceleration file despite including example of import.

Types incorrect or problem with webpack.

invert is not a function. This happens no matter how I import:
import invert from "invert-color" -> Runtime error: invert_color_1.default is not a function
import { invert } from "invert-color" -> Runtime error: invert_color_1.invert is not a function
import * as invert from "invert-color" -> Compiletime error: Cannot invoke an expression whose type lacks a call signature. Type 'typeof import("D:/.../node_modules/invert-color/lib/invert")' has no compatible call signatures.
import invert = require("invert-color") -> Compiletime error: Cannot invoke an expression whose type lacks a call signature. Type 'typeof import("D:/.../node_modules/invert-color/lib/invert")' has no compatible call signatures.

for it only works when use as (invert as any)(rgbArray) and I either import as
import * as invert from 'invert-color'
or
import invert = require('invert-color')

I'm using this with webpack in the browser.

Animation source code

@onury Can I have the source for the animation? I'm trying to do exactly what you did in the anim from this array of colors:

['289fec',
'999999',
'60ba7b',
'ff8a0d',
'da0004',
'666666',
'a4e4b4',
'ff3c00',
'990000',
'ff9594',
'ff9900',
'a4b442',
'39461a',
'3693bd',
'2294a2',
'33cc33',
'a29fe2',
'ff9900',
'618cff',
'cae1ff',
'333333',
'aaaaaa',
'f16f6f',
'f1504e',
'ff2200',
'444444',
'ffe64d',
'7f4f30',
'e7a115',
'ffa99a',
'776ba2',
'dddddd',
'776ba2']

Thanks!

Calculate dark / light colors shades from the inverted color

It's possible to setup some inference over the calculated inverted color to use its corresponding black and white shade, like it was converted from rgb to black and white in any sort of image editing program?

I was wondering that in cases where we want to calculate de inverted color but are not able to inform the proper shade of dark / light color that can combine well with the original color.

Feature proposal: consider exporting hexToRGB or other utility functions

Looking at source code, this library defines a hexToRGB function which might be useful to client code when working with colors. The idea would be to export this function, and maybe some other if it makes sense, without exposing all library implementation details. So that one can avoid copying code, or importing another library, just for that. Of course, this library should stay focused and not become a catch-all color utility. This suggestion is just because that function happens to be there already.

Just our 2¢. What do you think?
Regards

Error: Invalid HEX color: "yellow"

I'm building an app, where color is provided by data- attribute and it can be anything that user types. There is a potential case that they'll type "yellow"instead of "#ffff00".
Right now using this lib and color as word, will throw an error Error: Invalid HEX color: "yellow"
One possible solution I found is to map all cross-browser predefined colours to their hexadecimal counterpart:
https://stackoverflow.com/questions/1573053/javascript-function-to-convert-color-names-to-hex-codes/24390910
Do you think it'd be possible to include something like that into the lib itself?
PS Great module, by the way, super helpful!

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.