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

pretty-easy-rgb-to-hex

v1.0.4

Published

Converts an rgb color to it's corresponding HEX color value

Downloads

2,137

Readme

pretty-easy-rgb-to-hex

 

NPM Version Build Status - Travis CI Build Status - Appveyor Tests Dependancies

What is pretty-easy-rgb-to-hex?

pretty-easy-rgb-to-hex is a simple NodeJS module for converting an RGB(a) color value to it's corresponding HEX value.

 

Install

This is a NodeJS module available through the npm registry. Installation is done using the npm install command:

$ npm install pretty-easy-rgb-to-hex --save

 

Usage

After installing the module (localy in your project directory), in order to use it in your file you first need to require it.

let rgbToHEX = require('pretty-easy-rgb-to-hex');

 

or if you use TypeScript

import * as rgbToHEX from 'pretty-easy-rgb-to-hex';

 

The module returns a function for you to call and supply with an RGB(a) color value that you'd like to transform into its' corresponding HEX value. The function returns a String (HEX value, without a hash [#]) or an instance of Error class if the invalid color value was supplied to the function.  

Important :
  • HEX value returned does NOT include the hash [#] - this is intended!,
  • no matter if alpha is passed or not the resulting HEX value will be 6 characters long - this is due to the nature of valid HEX color values that are either 3 or 6 characters long,
  • HEX value returned is always 6 characters long,
  • HEX color value returned consists of CAPITAL letters (no lowercase letters are returned),
  • there are 4 valid ways of calling the function (see the example code below)

 

Examples

Convert RGB(a) color to HEX color

/*
*   The module is flexible enough, as it allows you
*   to supply it with different RGB(a) color values,
*   no matter if it is a String, Number values, Object...
*
*   There are 4 valid ways of calling the function
*/

//  Example 1. - single string value 
rgbToHEX('rgb(255, 0, 0)');         //  returns 'FF0000'
rgbToHEX('rgba(255, 0, 0, 1)');     //  returns 'FF0000'
rgbToHEX('255 0 0');                //  returns 'FF0000'
rgbToHEX('255, 0, 0, 1');           //  returns 'FF0000'

//  Example 2. - 3 string values
rgbToHEX('255', '0', '0');          //  returns 'FF0000'

//  Example 3. - 3 number values
rgbToHEX(255, 0, 0);                //  returns 'FF0000'

//  Example 4. - RGB(a) object
rgbToHEX({red: '255', green: '0', blue: '0'});              //  returns 'FF0000'
rgbToHEX({red:   255, green:   0, blue:  0 });              //  returns 'FF0000'
rgbToHEX({red:   255, green:   0, blue:  0 , alpha: 60});   //  returns 'FF0000'

 

Consider the following

The module will return an instance of an Error class, if argument passed is not a valid RGB(a) color value, instead of throwing an error and terminating the Node process thus making it more dynamic and usable in production where you depend on the user input.

Having this in mind, I advise you to consider including a utility library, to check the output data type, such as pretty-easy-data-types.

/*
*   Only import the checks you will be using,
*   instead of including the whole library
*/
const { 
    isString,       //  check if the value supplied is of type String
    isError         //  check for instance of an Error class
} = require('pretty-easy-data-types');
const rgbToHEX  = require('pretty-easy-rgb-to-hex');


//  You can pass any value/data type to a function, 
//  without causing your process to break
const convertToHex = rgbToHEX('this is an invalid value!');

/*
*   After converting the RGB(a) color to its' corresponding HEX value
*   you should perform the check on the value returned and see
*   if the conversion was successful.
*
*   If the value returned is of type String the conversion was successful
*   and in this example we're going to prepend the hash symbol [#]
*   else it is an instance of an Error class
*   and we're just going to log it to the console
*/
const hexColor = isString(convertToHex) ?
    `#${convertToHex}` :
    convertToHex;
if(isError(hexColor)) console.log(`Invalid RGB(a) color value passed: ${hexColor.message}`);

 

Want to contribute?

Great! Anyone can help make this project better - check out the github repository!

Found a bug?

Please open a an issue.

License

Copyright (c) 2017 Ognjen Jevremović

Licensed under the MIT License.