Code Monkey home page Code Monkey logo

pigment's Introduction

Pigment - A simple PHP library for color generation and manipulation

Pigment is a simple PHP library for color generation and manipulation. It can be used to generate gradients from two hexadecimal sources. It can also be used to manipulate colors (e.g. to make them lighter or darker).

Installation

You can install this library via Composer:

composer require c4n4r/pigment

Usage

Create a pigment instance

use Pigment\Pigment;
$pigment = new Pigment('#ff0000');

//or generate a random color

$pigment = Pigment::random();

$pigment->getHex(); // #ff0000
$pigment->getRgb(); // ["red" => 255, "green" => 0, "blue" => 0]

Darken or lighten a color

use Pigment\Pigment;

//darken by 10%
$pigment = new Pigment('#007D64');
$pigment->darken(10); //#00715a

//lighten by 10%
$pigment = new Pigment('#007D64');
$pigment->lighten(10); //#008a6e

You can also generate a gradient between two colors

use Pigment\Pigment;
$colorOne = new Pigment('#007D64');
$colorTwo = new Pigment('#ff0000');

//create a gradient with 10 steps
$gradient = $colorOne->gradient($colorTwo, 10);

Every method that manipulates a color returns a new Pigment class instance.

Use methods directly from the PigmentColorHandler class

use Pigment\Handlers\PigmentColorHandler;

$colorHandler = new PigmentColorHandler();

//darken by 10%
$darkenedColor = $colorHandler->darken('#007D64', 25);
$lightenedColor = $colorHandler->lighten('#007D64', 25);

//create a gradient with 10 steps
$gradient = $colorHandler->createGradientBetweenToColors('#007D64', '#ff0000', 10);

Those methods do not return a new Pigment class instance, you can use them if you don't need to manipulate the color further.

Transform colors in different formats

You can use the ColorTransformer class to transform colors in different formats without using a Pigment class.

$colorTransformer = new ColorTransformer();

//hex to rgb
$colorTransformer->explodeToRgb('#007D64');

//rgb to hex
$colorTransformer->implodeToHex([0, 125, 100]);

//rgb to hsl
$colorTransformer->rgbToHsl([0, 125, 100]);

//hsl to rgb
$colorTransformer->hslToRgb();

Examples

you can refer to the examples folder for more examples.

License

This library is licensed under the MIT license.

Credits

This library is developed by Hadrien Delphin, feel free to contact me if you have any question.

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.