A series of methods that let you manipulate colors. Just incase you ever need different shades of one color on the fly.

Related tags

Laravel phpColors
Overview

PHPColors Build Status

A series of methods that let you manipulate colors. Just incase you ever need different shades of one color on the fly.

Requirements

PHPColors requires PHP version 7.2.0 or greater.

Installation

Composer

Simply add mexitek/phpcolors to composer.json using dev-master.

composer require mexitek/phpcolors:dev-master

How it works

Instantiate an object of the color class with a hex color string $foo = new Color("336699"). That's it! Now, call the methods you need for different color variants.

Available Methods

  • darken( [$amount] ) : Allows you to obtain a darker shade of your color. Optionally you can decide to darken using a desired percentage.
  • lighten( [$amount] ) : Allows you to obtain a lighter shade of your color. Optionally you can decide to lighten using a desired percentage.
  • mix($hex, [$amount] ) : Allows you to mix another color to your color. Optionally you can decide to set the percent of second color or original color amount is ranged -100...0...100.
  • isLight( [$hex] ) : Determins whether your color (or the provide param) is considered a "light" color. Returns TRUE if color is light.
  • isDark( [$hex] ) : Determins whether your color (or the provide param) is considered a "dark" color. Returns TRUE if color is dark.
  • makeGradient( [$amount] ) : Returns an array with 2 indices light and dark, the initial color will either be selected for light or dark depending on its brightness, then the other color will be generated. The optional param allows for a static lighten or darkened amount.
  • complementary() : Returns the color "opposite" or complementary to your color.
  • getHex() : Returns the original hex color.
  • getHsl() : Returns HSL array for your color.
  • getRgb() : Returns RGB array for your color.

Auto lightens/darkens by 10% for sexily-subtle gradients

/**
 * Using The Class
 */

use Mexitek\PHPColors\Color;

// Initialize my color
$myBlue = new Color("#336699");

echo $myBlue->darken();
// 1a334d

echo $myBlue->lighten();
// 8cb3d9

echo $myBlue->isLight();
// false

echo $myBlue->isDark();
// true

echo $myBlue->complementary();
// 996633

echo $myBlue->getHex();
// 336699

print_r( $myBlue->getHsl() );
// array( "H"=> 210, "S"=> 0.5, "L"=>0.4 );

print_r( $myBlue->getRgb() );
// array( "R"=> 51, "G"=> 102, "B"=>153 );

print_r($myBlue->makeGradient());
// array( "light"=>"8cb3d9" ,"dark"=>"336699" )

Static Methods

  • hslToHex( $hsl ) : Convert a HSL array to a HEX string.
  • hexToHsl( $hex ) : Convert a HEX string into an HSL array.
  • hexToRgb( $hex ) : Convert a HEX string into an RGB array.
  • rgbToHex( $rgb ) : Convert an RGB array into a HEX string.
/**
 * On The Fly Custom Calculations
 */

use Mexitek\PHPColors\Color;

 // Convert my HEX
 $myBlue = Color::hexToHsl("#336699");

 // Get crazy with the HUE
 $myBlue["H"] = 295;

 // Gimme my new color!!
 echo Color::hslToHex($myBlue);
 // 913399

CSS Helpers

  • getCssGradient( [$amount] [, $vintageBrowsers] ) : Generates the CSS3 gradients for safari, chrome, opera, firefox and IE10. Optional percentage amount for lighter/darker shade. Optional boolean for older gradient CSS support.

Would like to add support to custom gradient stops

use Mexitek\PHPColors\Color;

// Initialize my color
$myBlue = new Color("#336699");

// Get CSS
echo $myBlue->getCssGradient();
/* - Actual output doesn't have comments and is single line

  // fallback background
  background: #336699;

  // IE Browsers
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8cb3d9', endColorstr='#336699');

  // Safari 5.1+, Mobile Safari, Chrome 10+
  background-image: -webkit-linear-gradient(top, #8cb3d9, #336699);

  // Standards
  background-image: linear-gradient(to bottom, #8cb3d9, #336699);

*/

However, if you want to support the ancient browsers (which has negligible market share and almost died out), you can set the second parameter to TRUE. This will output:

use Mexitek\PHPColors\Color;
$myBlue = new Color("#336699");

// Get CSS
echo $myBlue->getCssGradient(10, TRUE);
/* - Actual output doesn't have comments and is single line

  background: #336699; // fallback background
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8cb3d9', endColorstr='#336699'); // IE Browsers
  background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#8cb3d9), to(#336699)); // Safari 4+, Chrome 1-9
  background-image: -webkit-linear-gradient(top, #8cb3d9, #336699); // Safari 5.1+, Mobile Safari, Chrome 10+
  background-image: -moz-linear-gradient(top, #8cb3d9, #336699); // Firefox 3.6+
  background-image: -o-linear-gradient(top, #8cb3d9, #336699); // Opera 11.10+
  background-image: linear-gradient(to bottom, #8cb3d9, #336699); // Standards

*/

Github Contributors

  • mexitek
  • danielpataki
  • alexmglover
  • intuxicated
  • pborreli
  • curtisgibby
  • matthewpatterson
  • there4
  • alex-humphreys
  • zaher
  • primozcigler
  • thedavidmeister
  • tylercd100
  • Braunson

License

See LICENSE file or arlo.mit-license.org

Comments
  • Fix PHP 8.1 float to int conversion warnings

    Fix PHP 8.1 float to int conversion warnings

    On PHP 8.1, when a float value is passed to dechex, it triggers a deprecation log:

    Error E_DEPRECATED in ... generated by file /var/glpi/vendor/mexitek/phpcolors/src/Mexitek/PHPColors/Color.php on line 152:
    Implicit conversion from float 165.75 to int loses precision
    Error E_DEPRECATED in ... generated by file /var/glpi/vendor/mexitek/phpcolors/src/Mexitek/PHPColors/Color.php on line 153:
    Implicit conversion from float 165.75 to int loses precision
    Error E_DEPRECATED in ... generated by file /var/glpi/vendor/mexitek/phpcolors/src/Mexitek/PHPColors/Color.php on line 154:
    Implicit conversion from float 165.75 to int loses precision
    

    To fix this, I used the round function, as, IMHO, the produced value will be more relevant, but in facts, PHP was striping the decimal part of the number, which corresponds to the ceil behaviour. For instance, Color::hslToHex(['H' => 0, 'L' => 0, 'S' => 0.65]) will produce #a5a5a5 without this change, and #a6a6a6 with this change.

    opened by cedric-anne 16
  • Is there RGBA support?

    Is there RGBA support?

    Hi @mexitek,

    Thanks for this handy class. I was wondering if this class also supports rgba format and some functions for manpulating it. e.g. changeOpacity(). It would be great if these features could be added into your class.

    Thanks!

    opened by daviedR 8
  • rgbToHex has problems with RGB value '0'

    rgbToHex has problems with RGB value '0'

    It outputs single '0' in hex value. It should be a double zero '00'.

    You need to replace Line 197: return implode( '', $hex ); with: return sprintf("%02s%02s%02s", $hex[0], $hex[1], $hex[2]);

    hacktoberfest 
    opened by paulgeisler 6
  • Bugfix and tests for rgbToHex, new utility rgbToString.

    Bugfix and tests for rgbToHex, new utility rgbToString.

    This PR:

    • Fixes https://github.com/mexitek/phpColors/issues/25 - a bug where rgbToHex has problems converting RGB value '0'.
    • implements tests for rgbToHex in colorConvert.phpt.
    • adds utility method rgbToString, to convert an RGB array to its CSS string representation.
    • fixes whitespace, indentation, & commenting typos.
    opened by sqsinger1123 5
  • Improved the output of the getCssGradient method

    Improved the output of the getCssGradient method

    I changed quite some things in the getCssGradient method, here's the list:

    • added the $vintageBrowsers flag in the params, so only the recent vendor prefixes are used by default (but user can still fallback to the old support of needed)
    • this is really important: added the standard linear-gradient syntax, which is supported by the majority of the browsers today
    • removed the -ie-linear-gradient (this thing never existed)
    • updated the readme and the demo
    opened by primozcigler 5
  • prefix and suffix for getCssGradient helper

    prefix and suffix for getCssGradient helper

    i add this feature for my project, maybe it useful :) i just add prefix and suffix option for every line, for example \t for prefix and \n for suffix would be good :)

    opened by intuxicated 5
  • Composer version differs from this repository

    Composer version differs from this repository

    I might be wrong, but I couldn't manage to get the code from composer that would match this repository. I tried "composer require mexitek/phpColors".

    It's not much of an issue with the library, which is great, but I think someone should have a look at it.

    opened by pawelkaczmarek 4
  • Exclude dev files from dist package

    Exclude dev files from dist package

    For the time being, when fetching the dist package from composer, all repository files are included. Using export-ignore git attribute would permit to make the dist package ligther (see https://php.watch/articles/composer-gitattributes#export-ignore).

    Ignore files weight: 42.0kB Remaining files weight: 40.3kB

    opened by cedric-anne 3
  • Enhance precision in HSL to HEX conversion

    Enhance precision in HSL to HEX conversion

    Conversion from HSL to HEX of a grey color produces unexpected result in some cases, due to a rude int casting.

    For instance, converting a hsl(0, 0, 65%) will produces rgb(165.75, 165.75, 165.75). Casting these values to an int will just strip their floating part (i.e 165.75 -> 165), while a round operation would result in a more appropriate result.

    opened by cedric-anne 3
  • Error in importing Color.php

    Error in importing Color.php

    This is the error I am getting, using the Color.php

    require_once DIR . '/../PHPColors/Color.php'; This is the only thing I am using, in my php file.


      | Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) in /home/username/public_html/PHPColors/Color.php on line 38
     
    opened by tekken3vish 3
  • Incorrect conversion from HEX to RGB

    Incorrect conversion from HEX to RGB

    A yellow color rgb(244, 231, 15) is not converted correctly to HEX value. #f4e70f is the correct HEX value, but it is converted to #f4e7f instead. What is noticeable is that again zeroes (0) are involved, not at the beginning or end of the HEX value string but in between.

    opened by strarsis 3
  • Cant get it working

    Cant get it working

    Im a front end dev here...

    I cant seem to get this working at all. I just wanted to change a hex value to hsl and I'm going down a rabbit hole.

    $color = new Color($stripe_color);

    Why wont this work? It needs to be a string right?

    opened by saltnpixels 7
  • Method chaining

    Method chaining

    Thanks for this nice library! One thing that I constantly encounter is that I can't perform multiple actions (lighten, mix, ...) on one color but instead need to recreate Color objects for each step. I would suggest to return $this instead self::hslToHex()? This would break compatibility, so it would need an major version jump? Would go well though with @finwe's pull request #35.

    hacktoberfest 
    opened by DevDavido 2
Releases(v1.0.4)
Need some filters? This package is based on the Repository Design Pattern to let you create specific queries easily.

DevMakerLab/Laravel-Filters Need some filters? This package is based on the Repository Design Pattern to let you create specific queries easily. Insta

DevMakerLab 19 Feb 20, 2022
These are simple array and object collections that provide convinient methods to manipulate them.

Simple Collections These are simple array and object collections that provide convinient methods to manipulate collections; To install this package ty

Artem 4 Nov 19, 2021
Make custom helper just with one command!

Laravel Custom Helper Make custom helper just with one command! Very light!!! Installation requires Laravel 8+ Via composer: $ composer require Ranjba

Ali Ranjbar 2 Aug 23, 2022
The best squirrel tracker. Ever. (A demo app for LaravelSF meetups)

LaraSqrrl Identify and track squirrels via text, now using AWS Rekognition! Created for the November 10th, 2015; February 9th, 2016; May 10th, 2016; a

Paul Foryt 3 Mar 29, 2022
An easy way to add colors in your CLI scripts.

COLORS Here is a preview of what you can achieve with the library: Installation Installation via composer is highly recommended. { "require": {

Kevin Le Brun 335 Dec 4, 2022
Takes in a HEX color and produces variations of that colour for the foreground and background

css-colors Takes in a HEX color and produces variations of that colour for the foreground and background It takes a great php class made by Patrick Fi

Soapbox Innovations Inc. 9 Jul 24, 2020
Multipurpose Laravel and Livewire Application. This is a part of tutorial series on Youtube.

Multipurpose Laravel and Livewire Application This is a part of YouTube tutorial series on building application using Laravel and Livewire. Here is th

Clovon 88 Dec 23, 2022
A plugin for Blessing Skin Server that can let you display Google Ads with Google AdSense in the website.

A plugin for Blessing Skin Server that can let you display Google Ads with Google AdSense in the website.

Big_Cake 2 Jan 25, 2022
A kernel designed to run one and only one application in a virtualized environment

nanos Nanos is a new kernel designed to run one and only one application in a virtualized environment. It has several constraints on it compared to a

NanoVMs 2k Dec 20, 2022
This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.

Laravel Verify New Email Laravel supports verifying email addresses out of the box. This package adds support for verifying new email addresses. When

Protone Media 300 Dec 30, 2022
One-to-one plugin for editing world chat messages.

WorldChat One-to-one plugin for editing world chat messages. Supports English and Turkish language To set a new world chat format /worldchat new "worl

Aydın Demirci 12 May 20, 2023
Is an Extension of Laravel View Class which compiles String Template on the fly. It automatically detects changes on your string template and recompiles it if needed.

Laravel-fly-view Is an Extension of Laravel View Class which compiles String Template on the fly. It automatically detects changes on your string temp

John Turingan 16 Jul 17, 2022
Let's base your Laravel project with PROS style

Purpose This library is for convenient methods that use to register code base for Laravel project Target We aimed to reduce complexity for real projec

Protean Studios Co., Ltd. 4 Mar 29, 2022
Worlds (soon to be) most advanced Anime site! Featuring Administration features and everything you need for users and yourself. The successor of aniZero.

/**********************************************************************\ | _____ H33Tx & xHENAI __ 31.01.2022| |

HENAI.eu 40 Jan 3, 2023
Aliyun oss filesystem storage adapter for laravel 5. You can use Aliyun OSS just like laravel Storage as usual

Aliyun oss filesystem storage adapter for laravel 5. You can use Aliyun OSS just like laravel Storage as usual

jacob 517 Dec 29, 2022
The list of all Algerian provinces and cities according to the official division in different formats: csv, xlsx, php, json, etc.

algeria-cities This repository contains the list of all the administrative provinces and cities in Algeria. The data is up-to-date according to the of

Ramtani Othmane 393 Jan 2, 2023
This package can use form request just as Laravel do.

Lumen Form Request This package can use form request just as Laravel do. Installation Install by composer $ composer require chhw/form-request In

null 2 Nov 17, 2021
Quickly identify controller methods with no route in your Laravel applications.

Orphan Controller Quickly identify controller methods with no route in your Laravel applications. Installation You can install the package via Compose

Ryan Chandler 16 Feb 18, 2022
Control frontend access to properties/methods in Livewire using PHP 8 attributes.

This package adds PHP 8.0 attribute support to Livewire. In specific, the attributes are used for flagging component properties and methods as frontend-accessible.

ARCHTECH 83 Dec 17, 2022