PHP code example of daikazu / laratone

1. Go to this page and download the library: Download daikazu/laratone 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/ */

    

daikazu / laratone example snippets


return [
    // Table prefix for Laratone tables
    'table_prefix' => 'laratone_',

    // Cache duration in seconds for color books and colors
    'cache_time' => 3600,

    // Reference white point for LAB color calculations
    // Options: 'D50' (print), 'D55', 'D65' (daylight, default), 'D75'
    'white_point' => 'D65',

    // Store calculated RGB/CMYK/LAB/OKLCH values when saving a color
    'pre_calculate_colors' => false,

    // Default algorithm for finding closest colors: 'lab', 'ciede2000' or 'oklch'
    'default_match_algorithm' => 'lab',

    // Maximum number of colors that find-closest and search can return
    'max_match_limit' => 100,

    // API rate limit as "maxAttempts,decayMinutes" (null to disable)
    'rate_limit' => '60,1',

    // REST API routes: turn them off, or change the URL prefix
    'routes' => [
        'enabled' => true,
        'prefix'  => 'api/laratone',
    ],
];

// config/laratone.php
'routes' => [
    'enabled' => true,          // false = don't register any Laratone routes
    'prefix'  => 'colors/v1',   // endpoints become /colors/v1/colorbooks, ...
],

// config/laratone.php
'rate_limit' => '120,1', // 120 requests per minute
'rate_limit' => null,    // disable the built-in throttle

// app/Providers/AppServiceProvider.php
use Illuminate\Routing\Router;

public function boot(Router $router): void
{
    $router->aliasMiddleware('laratone', \App\Http\Middleware\YourCustomMiddleware::class);
}

// app/Http/Middleware/LaratoneApiMiddleware.php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Routing\Middleware\ThrottleRequests;

class LaratoneApiMiddleware extends ThrottleRequests
{
    public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')
    {
        // Add custom logic here (authentication, logging, etc.)
        return parent::handle($request, $next, $maxAttempts, $decayMinutes, $prefix);
    }
}

use Daikazu\Laratone\Facades\Laratone;

// Get all color books with colors
$colorBooks = Laratone::colorBooks();

// Get a specific color book by slug
$colorBook = Laratone::colorBookBySlug('color-book-plus-solid-coated');

// Create a new color book
$newColorBook = Laratone::createColorBook('My New Color Book');

// Create with custom slug
$newColorBook = Laratone::createColorBook('My Color Book', 'custom-slug');

// Add a single color to a color book (only hex => 'FF0000'],
    ['name' => 'Green', 'hex' => '00FF00'],
    ['name' => 'Blue', 'hex' => '0000FF'],
]);

// Get all colors from a color book
$colors = Laratone::getColorsFromBook($colorBook);

// Update a color
Laratone::updateColor($color, ['name' => 'Updated Color Name']);

// Delete a color
Laratone::deleteColor($color);

// Find closest matching colors to a target hex
$closest = Laratone::findClosestColors($colorBook, 'FF5500');
// Returns the single closest color by default

// Find multiple closest colors with specific algorithm
$closest = Laratone::findClosestColors(
    colorBook: $colorBook,
    targetHex: 'FF5500',
    limit: 5,
    algorithm: 'ciede2000'  // 'lab' (default), 'ciede2000' or 'oklch'
);

// Each result 

use Daikazu\Laratone\Models\Color;

$color = Color::first();

// Access color values as arrays
$color->hex;   // 'FF0000' (b' => 67.22] (stored or calculated)
$color->cmyk;  // ['c' => 0, 'm' => 100, 'y' => 100, 'k' => 0] (stored or calculated)
$color->oklch; // ['l' => 0.6279, 'c' => 0.2577, 'h' => 29.23] (stored or calculated)

// Access the parent color book
$colorBook = $color->colorBook;

Laratone::clearCache();
bash
php artisan vendor:publish --tag="laratone-config"
bash
php artisan vendor:publish --tag="laratone-migrations"
php artisan migrate
bash
php artisan about --only=laratone
bash
php artisan laratone:seed
bash
php artisan laratone:seed ColorBookPlusSolidCoatedSeeder
bash
php artisan laratone:seed --file ./mycolorbookfile.json
bash
php artisan laratone:clear-cache