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

@avolutions/colorado

v0.2.0

Published

Colorado is a lightweight, intuitive JavaScript library for handling and converting color values. Seamlessly switch between RGB and Hex formats with simple methods and generate clean string outputs for your web projects. Perfect for developers looking for

Downloads

128

Readme

Colorado

Tests npm Version License

About

Colorado is a lightweight, intuitive JavaScript library for handling and converting color values. Seamlessly switch between RGB and Hex formats with simple methods and generate clean string outputs for your web projects. Perfect for developers looking for an easy and dynamic color management solution.

Installation

To install the Colorado package, run the following command via npm:

npm install @avolutions/colorado

Colorado is designed to be flexible and works with both CommonJS and ES Modules. You can include it in your project using either of these systems based on your environment.

For CommonJS:

const Color = require('@avolutions/colorado');

For ES Modules:

import { Color } from '@avolutions/colorado';

Usage

Colorado provides a straightforward API for managing and converting colors in JavaScript. Whether you're working with Hex codes or RGB values, you can easily create and convert between formats with just a few methods. Below are some common use cases and examples to help you get started.

Create colors

Creating new color objects with Colorado is simple using the Color constructor. You can initialize a new color by passing either RGB values or a Hex code as an argument.

Using RGB values

You can create a color by passing three integer values for the red, green, and blue components. Each value should be an integer between 0 and 255, representing the intensity of each color channel.

const red = new Color(255, 0, 0);

Using a Hex code

You can create a color by passing a 6-digit or 3-digit Hex code. It accepts both upper and lowercase letters.

const red = new Color('#FF0000'); // same as #ff0000
const green = new Color('#0f0'); // same as #00ff00

Checking Color Format

Colorado provides simple methods to check whether a color is in RGB or Hex format using the isRgb() and isHex() methods. These methods are useful when you need to validate the format of the color or ensure you're working with the right type before performing conversions or operations.

Checking if the Color is RGB

The isRgb() method returns true if the color is in RGB format, otherwise it returns false.

const red = new Color(255, 0, 0).isRgb(); // true
const green = new Color('#00FF00').isRgb(); // false

Checking if the Color is Hex

Similarly, the isHex() method checks if the color is in Hex format and returns true if it is, otherwise false.

const red = new Color('#FF0000').isHex(); // true
const green = new Color(0, 255, 0).isHex(); // false

Converting Colors

Colorado makes it simple to convert between RGB and Hex formats using the built-in methods toRgb() and toHex(). These methods allow you to easily switch between the two color representations as needed for your project.

Converting RGB to Hex

If you have a color in RGB format, you can use the toHex() method to convert it to a Hex color.

const red = new Color('255, 0, 0').toHex(); // #FF0000

Converting Hex to RGB

Similarly, you can convert a Hex color to a RGB color using the toRgb() method. This works whether the Hex code is in 6-digit or 3-digit format, and regardless of case.

const red = new Color('#FF0000').toRgb(); // 255, 0, 0
const green = new Color('#0f0').toRgb(); // 0, 255, 0

No Conversion Needed

If you attempt to convert a color from RGB to RGB or from Hex to Hex, nothing will happen. The methods toRgb() and toHex() will simply return the color in the same format if it’s already in the desired form.

const red = new Color('255, 0, 0').toRgb(); // 255, 0, 0
const green = new Color('#00ff00').toHex(); // #00ff00

Outputting Colors as Strings

The toString() method in Colorado provides an easy way to output a color in string format. Depending on whether the color is in RGB or Hex format, the toString() method will return the appropriate representation.

RGB Colors

If the color is in RGB format, toString() will return a string in the format rgb(r, g, b), where r, g, and b are the integer values for the red, green, and blue channels.

const red = new Color(255, 0, 0).toString(); // 'rgb(255, 0, 0)'
const green = new Color('#00ff00').toRgb().toString(); // 'rgb(0, 255, 0)'

Hex Colors

If the color is in Hex format, toString() will return the Hex code as a string, including the # symbol. The output will match the original format (6-digit or 3-digit, and either upper or lowercase).

const red = new Color('#FF0000').toString(); // '#FF0000'
const green = new Color(0, 255, 0).toHex().toString(); // '#00FF00'

License

This project is licensed under the MIT License. For more details, see the LICENSE file included in the repository.