PHP code example of byteforge / color-randomizer

1. Go to this page and download the library: Download byteforge/color-randomizer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

byteforge / color-randomizer example snippets


use ByteForge\ColorRandomizer\ColorRandomizer;
 
// Example code...

// Generating a color without its name
$color = ColorRandomizer::getSingleColor(hex: true, minBrightness: 30, maxBrightness: 255, withMostMatchedName: false);

// Generating a color with its name
$color = ColorRandomizer::getSingleColor(hex: true, minBrightness: 30, maxBrightness: 255, withMostMatchedName: true, formattedName: true);

// Other Codes..
$colors = ColorRandomizer::getMultipleColors(colorsAmount: 4, hex: true, minBrightness: 30, maxBrightness: 255, withMostMatchedName: true, formattedName: true); // returns an array like, [['name'=> Blue, 'color_code' => '#0000FF'], ['name' => 'Alice Blue', 'color_code' => '#F0F8FF' ...]]

$colors = ColorRandomizer::getMultipleColors(colorsAmount: 4, hex: true, minBrightness: 30, maxBrightness: 255, withMostMatchedName: false) ;// returns an array like, ['#ED872D', '#483D8B', '#DE3163' ...]

// 1st way
$colors = ColorRandomizer::getColors(formatNames: true); // You are able to Format Names through The function, This makes it better than the 2nd way.

// 2nd way
$colors = ColorRandomizer::$colors; // Color Names wont be formatted

// RGB To Hex

// 1st Way
$hex = ColorRandomizer::rgbToHex(fullRGB: "5, 91, 230"); // Make Sure You Specify The Argument Name Like 'fullRGB: $myRGB'. You can also give the RGB like rgb(5, 91, 230),
// Returns #055be6

// 2nd Way
$hex = ColorRandomizer::rgbToHex(r: 5, g: 91, b: 230); // Make Sure You Specify The Argument Name Like 'r:, g: and b:',
// Returns #055be6

// Hex To RGB
$rgb = ColorRandomizer::hexToRgb(hex: '#055be6');
// returns rgb(5, 91, 230)

// Get Color Difference Between 2 colors
$colorDifference = ColorRandomizer::colorDifference(color1: '#0548e6', color2: '#05a6e6', hex: true); // Hex refers that your color1 and color2 is Hex or RGB.
// returns 94. Similar Colors Threshold Could Be 130

// Get Color Brightness
$colorBrightness = ColorRandomizer::calculateBrightness(rgbOrHex: '#0548e6', type: 'hex'); // type must be hex or rgb, Its for verifying your Color code Type.
// returns 69


$rgbArray = ColorRandomizer::extractRGBComponents(colorString: "rgb(5, 91, 230)") // returns ['r' => 5, 'g' => 91, 'b' => 230'];

$colorCode = ColorRandomizer::getCodesFromHtmlColors(hex: true, name: 'dandelion') // returns #F0E130;

$colorName = ColorRandomizer::getNearestNameFromCode(hex: true, rgbOrHex: '#05e676'); // returns 'spring green'
// You can use ucwords($colorName) to format the words 1st letter to uppercase

$similarColors = ColorRandomizer::getSimilarColors(rgbOrHex: '#FFB7C5', goalAmount: 7, hex: true, maxDifference: 100, minDifference: 0.5, allowCustomColors: false);
/* It'll return an array containing other arrays like,
[["name" => "Carnation Pink", "hex" => "FFA6C9", "rgb" => "rgb(255, 166, 201)", "difference" => 17.464249196573] ... more]
*/ 

COLOR_RANDOMIZER_COLOR_PAGE_ROUTE = '/all-colors'
COLOR_RANDOMIZER_COLOR_PAGE = true