Giter Club home page Giter Club logo

randomcolor.php's Introduction

Random Color

PHP version

For generating attractive random colors.

This is a PHP port of David Merfield randomColor Javascript utility.

See the results on the demo.

Demo

Options

You can pass an options object to influence the type of color it produces. The options object accepts the following properties:

hue – Controls the hue of the generated color. You can pass a string representing a color name (e.g. 'orange'). Possible color names are red, orange, yellow, green, blue, purple, pink and monochrome. You can also pass an array of multiple hues or a specific hue (0 to 360).

luminosity – Controls the luminosity of the generated color. You can pass a string containing bright, light or dark.

format – A string which specifies the format of the generated color. Possible values are hsv, hsl, hslCss, rgb, rgbCss, and hex.

prng – A random (or not) number generator. mt_rand is used as default one.

Examples

use \Colors\RandomColor;

// Returns a hex code for an attractive color
RandomColor::one(); 

// Returns an array of ten green colors
RandomColor::many(10, array(
   'hue' => 'green'
));

// Returns a hex code for a light blue
RandomColor::one(array(
   'luminosity' => 'light',
   'hue' => 'blue'
));

// Returns one yellow or blue color
RandomColors::one(array(
    'hue' => array('yellow', 'blue')
));

// Returns a hex code for a 'truly random' color
RandomColor::one(array(
   'luminosity' => 'random',
   'hue' => 'random'
));

// Returns a bright color in RGB
RandomColor::one(array(
   'luminosity' => 'bright',
   'format' => 'rgbCss' // e.g. 'rgb(225,200,20)'
));

Other languages

RandomColor is available in JavaScript, C#, C++, Go, Haskell, Kotlin, Mathematica, Python, Swift, Perl6, Objective-C, Java, R, Reason, Dart, Ruby and Rust

License

This project is licensed under the terms of the MIT license.

randomcolor.php's People

Contributors

mistic100 avatar mundschenk-at avatar rieschl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

randomcolor.php's Issues

Deterministic Color Creation

It would be nice to have the option to create deterministic colors. Providing a seed for RandomColor (which will not interfere with the normal mt_random) would be really nice.

New functionality: not retrive similar colors

Hello,

When we use RandomColor::many(36);
We often get similar colors.
eg green, and darker green.

It could be useful to not retrive similar colors.

Let me know what you think. And if it's doable.

Produce color from hash

I would like to use your library to produce color from hash string?

For example, I have pie chart and I would like to assign color per each portion of it based on data for such portion.

I would like to hash within that portion and produce some color based on that hash.

Now I am facing problem that it always generates different color.

Implicit conversion Exception in PHP8.1

Hi @mistic100,
Here is an exception thrown with php8.1 calling RandomColor::one(['hue'=>['blue','yellow','pink','purple','orange','red','green']])

Implicit conversion from float 87.6 to int loses precision - #0 [internal function]: {closure}()
#1 \composer\vendor\mistic100\randomcolor\src\RandomColor.php(263): mt_rand()
#2 \composer\vendor\mistic100\randomcolor\src\RandomColor.php(165): Colors\RandomColor::_rand()
#3 \composer\vendor\mistic100\randomcolor\src\RandomColor.php(44): Colors\RandomColor::_pickBrightness()

My quick patch, but might be fix higher in the call stack

diff --git "a/src/RandomColor.php" "b/src/RandomColor.php"
index df96204..f1ee99b 100644
--- "a/src/RandomColor.php"
+++ "b/src/RandomColor.php"
@@ -260,7 +260,7 @@ class RandomColor
     }
     else
     {
-      return mt_rand($bounds[0], $bounds[1]);
+      return mt_rand(round($bounds[0]), round($bounds[1]));
     }
   }
 

Provide multiple hues

It would be nice if you could provide an array of hues, instead of just one or "random".

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.