color

Color parsing, RGB/HSL/Hex conversion, manipulation and WCAG contrast.

v1.0.0Math, time & IDsbundledimport "color";
Overview Installation Documentation Examples

Overview

Zero-dependency HolyPHP library for color parsing, conversion, manipulation, and WCAG 2.1 contrast calculations.

bundled — works offline zero dependencies rgb hsl

Quick start

import "color";

$brand = color_from_hex("#3b82f6");
println($brand->toRgbString());      // rgb(59, 130, 246)
println($brand->lighten(15.0)->toHex());
println($brand->darken(15.0)->toHex());

$white = color_from_hex("#ffffff");
$ratio = $brand->contrastRatio($white);
println("Contrast ratio: " . $ratio);

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install color

This adds the library to hphp.yaml, installs it into .hphp/packages/color/ and pins the exact version in hphp.lock:

require:
  color: "^1.0"

Use it

import "color";

color is bundled with the compiler, so import "color"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install color          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install color
Packagecolor
Version1.0.0 (follows the compiler)
Entry filecolor.hphp
Sourcepackages/lib/color ↗

Documentation

API Reference

Factory Functions

Color Class

Properties

Methods

Error Handling

All invalid inputs throw Exception with descriptive color: prefixed messages:

Examples

01Quick start

Bundled with the compiler — import "color"; works offline in any file, or add it to a project with hphp install color.

import "color";

$brand = color_from_hex("#3b82f6");
println($brand->toRgbString());      // rgb(59, 130, 246)
println($brand->lighten(15.0)->toHex());
println($brand->darken(15.0)->toHex());

$white = color_from_hex("#ffffff");
$ratio = $brand->contrastRatio($white);
println("Contrast ratio: " . $ratio);
Try it
hphp install color
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_color.hphp.