Home Laravel How To Get Color Code from Color Name In Laravel ?

How To Get Color Code from Color Name In Laravel ?

by Shahriar Sagor
Get Color Code from Color Name

Today now in this post i will show you How To Get Color Code from Color Name In Laravel ? Now in this tutorial i be will provide you an example of Laravel Get Color Code from the Color Name Example. We know it’s a simple example of How we can Get Color Code from the Name in Laravel? Here I will like to share with you an example of How to retrieve the human name of a color by its hex code in laravel application. So it’s a simple example of how to get color code from the name in laravel blade. We will do the following things for How to get Hexadecimal Color Code by using the regular name in laravel.

Here i will give you a simple example of laravel find the color code to color name Code Example

So let’s start the following example:

Step 1: Download Laravel

Let us the begin the tutorial by installing a new laravel application. if you have already then not created the project, then skip the following step.

composer create-project laravel/laravel example-app
Step 2: Create Routes



use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ColorNameController;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('color-code',[ColorNameController::class, 'index']);
Route::post('/color-code',[ColorNameController::class, 'colorCode'])->name('colorCode');
Step 3 : Create New Controller

Now in this second step, we have to create a new as name ColorNameController.

So let’s create the ColorNameController by the following command:

php artisan make:controller ColorNameController    



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ourcodeworld\NameThatColor\ColorInterpreter;

class ColorNameController extends Controller

     * Write Your Code..
     * @return string
    public function index()
        return view('index');

     * Write Your Code..
     * @return string
    public function colorCode(Request $request)
            'name' => 'required',

        $color_name = $request->name;

        $colors  = [
            'blanchedalmond '=>'FFEBCD',

        $color_name = strtolower($color_name);
        if (isset($colors[$color_name]))
            $colorCode = $colors[$color_name];
            return view('index',compact('colorCode'));
            $error = "color doesn't found";
            return view('index',compact('error'));

Read Also: How to Send Mail in PHP Laravel?

Step 4: Create Blade File


<!DOCTYPE html>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Laravel Get Color Code from Color Name Example - Codingspoint.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-10">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h3 class="text-center">Laravel Get Color Code from Color Name Example-Codingspoint.com</h3>
                    <div class="card-body">

                        <form action="{{ route('colorCode') }}" method="POST">
                            <div class="mt-3">
                                <label>Color Name : <span class="text-danger">*</span></label>
                                <input type="text" name="name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name') }}" placeholder="Enter Name">
                                    <span class="text-danger">{{ $message }}</span>

                                    <span class="text-danger">{{ $error }}</span>
                            <div class="text-center mt-3">
                                <button type="submit" class="btn btn-primary">Submit</button>

                            <div class="row my-3">
                                <div class="col-md-12 d-flex justify-content-center">
                                    <p>color code : <strong>#{{ $colorCode }}</strong></p>
                                <div class="col-md-12 d-flex justify-content-center">
                                        <div style="height: 100px; width: 100px; border: 1px solid black; background-color: #{{ $colorCode  }}; "></div>
Run Laravel App:

Thanks all steps have been done, now we need to type the given command and then hit enter to run the laravel application:

php artisan serve

Now, we need to open web browser, type the given URL and view the app output:


Read Also: Laravel 8 Sanctum API Authentication

Thanks for read. I hope it help you. For more you can follow us on facebook


You may also like