All Projects β†’ spatie β†’ Color

spatie / Color

Licence: mit
A little library to deal with color conversions

Projects that are alternatives of or similar to Color

Farge
🎈Tell the name of hex color
Stars: ✭ 23 (-86.14%)
Mutual labels:  hex, color, rgb
Colorpicker
A mininal but complete colorpicker desktop app
Stars: ✭ 766 (+361.45%)
Mutual labels:  hex, rgb, color
ColorHelper
No description or website provided.
Stars: ✭ 34 (-79.52%)
Mutual labels:  hex, color, rgb
khroma
A collection of functions for manipulating CSS colors, inspired by SASS.
Stars: ✭ 28 (-83.13%)
Mutual labels:  hex, color, rgb
Xcodecolorsense
🎈 An Xcode plugin that makes working with color easier
Stars: ✭ 79 (-52.41%)
Mutual labels:  hex, rgb, color
colorsys
🎨 Minimalistic color converter for RGB, HSV, HSL, CMYK, HEX and CSS strings
Stars: ✭ 53 (-68.07%)
Mutual labels:  hex, color, rgb
Culori
A comprehensive color library for JavaScript.
Stars: ✭ 271 (+63.25%)
Mutual labels:  hex, rgb, color
colorsys.rs
Lib for modifying colors and converting to other spaces
Stars: ✭ 28 (-83.13%)
Mutual labels:  hex, color, rgb
Colorhighlight
🎨 Lightweight Color Highlight colorizer for Sublime Text
Stars: ✭ 76 (-54.22%)
Mutual labels:  hex, rgb, color
color
A library of well-tested helper methods for working with colors.
Stars: ✭ 13 (-92.17%)
Mutual labels:  hex, color, rgb
hex-to-css-filter
Easy way to generate colors from HEX to CSS Filters
Stars: ✭ 20 (-87.95%)
Mutual labels:  hex, color, rgb
Values.js
πŸ‡ Get the tints and shades of a color
Stars: ✭ 97 (-41.57%)
Mutual labels:  hex, rgb, color
React Color Extractor
A React component which extracts colors from an image
Stars: ✭ 314 (+89.16%)
Mutual labels:  hex, rgb
Color Diff
Implemets the CIEDE2000 color difference algorithm, conversion between RGB and lab color and mapping all colors in palette X to the closest color in palette Y based on the CIEDE2000 difference.
Stars: ✭ 296 (+78.31%)
Mutual labels:  conversion, rgb
Swiftcolorgen
A tool that generate code for Swift projects, designed to improve the maintainability of UIColors
Stars: ✭ 152 (-8.43%)
Mutual labels:  rgb, color
Gradstop
JavaScript micro library to generate gradient color stops πŸ³οΈβ€πŸŒˆ
Stars: ✭ 144 (-13.25%)
Mutual labels:  hex, rgb
Xcodecolorsense2
πŸ‰ An Xcode source editor extension that shows hex color info
Stars: ✭ 281 (+69.28%)
Mutual labels:  hex, color
Hue
🎨 Hue is the all-in-one coloring utility that you'll ever need.
Stars: ✭ 3,306 (+1891.57%)
Mutual labels:  hex, color
Colorkit
Advanced color manipulation for iOS.
Stars: ✭ 388 (+133.73%)
Mutual labels:  hex, color
Unicopy
Unicode command-line codepoint dumper
Stars: ✭ 16 (-90.36%)
Mutual labels:  conversion, hex

A little library to handle color conversions

Latest Version on Packagist Software License Build Status Quality Score Total Downloads Tests

A little library to handle color conversions. Currently supports rgb, rgba, hex, hsl and hsla formats.

$rgb = Rgb::fromString('rgb(55,155,255)');

echo $rgb->red(); // 55
echo $rgb->green(); // 155
echo $rgb->blue(); // 255

echo $rgb; // rgb(55,155,255)

$rgba = $rgb->toRgba(); // `Spatie\Color\Rgba`
$rgba->alpha(); // 1
echo $rgba; // rgba(55,155,255,1)

$hex = $rgb->toHex(); // `Spatie\Color\Hex`
echo $hex; // #379bff

$hsl = $rgb->toHsl();
echo $hsl; // hsl(210,100%,100%)

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/color

Usage

The Color package contains a separate class per color format, which each implement a Color interface.

There are five classes which implement the Color interface:

  • Hex
  • Hsl
  • Hsla
  • Rgb
  • Rgba

interface Spatie\Color\Color

fromString(): Color

Parses a color string and returns a Color implementation, depending on the format of the input string.

Hex::fromString('#000000');
Rgba::fromString('rgba(255, 255, 255, 1)');
Hsla::fromString('hsla(360, 100%, 100%, 1)');

Throws an InvalidColorValue exception if the string can't be parsed.

Rgb, Rgba, Hsl and Hsla strings are allowed to have spaces. rgb(0,0,0) is just as valid as rgb(0, 0, 0).

red(): int|string

Return the value of the red color channel.

Hex::fromString('#ff0000')->red(); // 'ff'
Rgb::fromString('rgb(255, 0, 0)')->red(); // 255

green(): int|string

Return the value of the green color channel.

Hex::fromString('#00ff00')->green(); // 'ff'
Rgb::fromString('rgb(0, 255, 0)')->green(); // 255

blue(): int|string

Return the value of the blue color channel.

Hex::fromString('#0000ff')->blue(); // 'ff'
Rgb::fromString('rgb(0, 0, 255)')->blue(); // 255

toHex(): Hex

Convert a color to a Hex color.

Rgb::fromString('rgb(0, 0, 255)')->toHex();
// `Hex` instance; '#0000ff'

When coming from a color format that supports opacity, the opacity will simply be omitted.

toHsl(): Hsl

Convert a color to a Hsl color.

Rgb::fromString('rgb(0, 0, 255)')->toHsl();
// `Hsl` instance; 'hsl(240, 100%, 50%)'

When coming from a color format that supports opacity, the opacity will simply be omitted.

Rgba::fromString('rgba(0, 0, 255, .5)')->toHsl();
// `Hsl` instance; 'hsl(240, 100%, 50%)'

toHsla(float $alpha = 1): Hsla

Convert a color to a Hsla color.

Rgb::fromString('rgb(0, 0, 255)')->toHsla();
// `Hsla` instance; 'hsla(240, 100%, 50%, 1.0)'

When coming from a color format that doesn't support opacity, it can be added by passing it to the $alpha parameter.

Rgb::fromString('rgb(0, 0, 255)')->toHsla(.5);
// `Hsla` instance; 'hsla(240, 100%, 50%, 0.5)'

toRgb(): Rgb

Convert a color to an Rgb color.

Hex::fromString('#0000ff')->toRgb();
// `Rgb` instance; 'rgb(0, 0, 255)'

When coming from a color format that supports opacity, the opacity will simply be omitted.

Rgba::fromString('rgb(0, 0, 255, .5)')->toRgb();
// `Rgb` instance; 'rgb(0, 0, 255)'

toRgba(float $alpha = 1): Rgba

Convert a color to a Rgba color.

Rgb::fromString('rgb(0, 0, 255)')->toRgba();
// `Rgba` instance; 'rgba(0, 0, 255, 1)'

When coming from a color format that doesn't support opacity, it can be added by passing it to the $alpha parameter.

Rgba::fromString('rgb(0, 0, 255)')->toRgba(.5);
// `Rgba` instance; 'rgba(0, 0, 255, .5)'

__toString(): string

Cast the color to a string.

(string) Rgb::fromString('rgb(0, 0, 255)'); // 'rgb(0,0,255)'
(string) Rgba::fromString('rgb(0, 0, 255, .5)'); // 'rgb(0,0,255,0.5)'
(string) Hex::fromString('#0000ff'); // '#0000ff'
(string) Hsla::fromString('hsl(240, 100%, 50%)'); // 'hsl(240, 100%, 50%)'
(string) Hsla::fromString('hsla(240, 100%, 50%, 1.0)'); // 'hsla(240, 100%, 50%, 1.0)'

Factory::fromString(): Color

With the Factory class, you can create a color instance from any string (it does an educated guess under the hood). If the string isn't a valid color string in any format, it throws an InvalidColorValue exception.

Factory::fromString('rgb(0, 0, 255)'); // `Rgb` instance
Factory::fromString('#0000ff'); // `Hex` instance
Factory::fromString('hsl(240, 100%, 50%)'); // `Hsl` instance
Factory::fromString('Hello world!'); // `InvalidColorValue` exception

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].