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

routes/web.php

<?php

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    

app/Http/Controllers/ColorNameController.php

<?php

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)
    {
        $request->validate([
            'name' => 'required',
        ]);

        $color_name = $request->name;

        $colors  = [
            'aliceblue'=>'F0F8FF',
            'antiquewhite'=>'FAEBD7',
            'aqua'=>'00FFFF',
            'aquamarine'=>'7FFFD4',
            'azure'=>'F0FFFF',
            'beige'=>'F5F5DC',
            'bisque'=>'FFE4C4',
            'black'=>'000000',
            'blanchedalmond '=>'FFEBCD',
            'blue'=>'0000FF',
            'blueviolet'=>'8A2BE2',
            'brown'=>'A52A2A',
            'burlywood'=>'DEB887',
            'cadetblue'=>'5F9EA0',
            'chartreuse'=>'7FFF00',
            'chocolate'=>'D2691E',
            'coral'=>'FF7F50',
            'cornflowerblue'=>'6495ED',
            'cornsilk'=>'FFF8DC',
            'crimson'=>'DC143C',
            'cyan'=>'00FFFF',
            'darkblue'=>'00008B',
            'darkcyan'=>'008B8B',
            'darkgoldenrod'=>'B8860B',
            'darkgray'=>'A9A9A9',
            'darkgreen'=>'006400',
            'darkgrey'=>'A9A9A9',
            'darkkhaki'=>'BDB76B',
            'darkmagenta'=>'8B008B',
            'darkolivegreen'=>'556B2F',
            'darkorange'=>'FF8C00',
            'darkorchid'=>'9932CC',
            'darkred'=>'8B0000',
            'darksalmon'=>'E9967A',
            'darkseagreen'=>'8FBC8F',
            'darkslateblue'=>'483D8B',
            'darkslategray'=>'2F4F4F',
            'darkslategrey'=>'2F4F4F',
            'darkturquoise'=>'00CED1',
            'darkviolet'=>'9400D3',
            'deeppink'=>'FF1493',
            'deepskyblue'=>'00BFFF',
            'dimgray'=>'696969',
            'dimgrey'=>'696969',
            'dodgerblue'=>'1E90FF',
            'firebrick'=>'B22222',
            'floralwhite'=>'FFFAF0',
            'forestgreen'=>'228B22',
            'fuchsia'=>'FF00FF',
            'gainsboro'=>'DCDCDC',
            'ghostwhite'=>'F8F8FF',
            'gold'=>'FFD700',
            'goldenrod'=>'DAA520',
            'gray'=>'808080',
            'green'=>'008000',
            'greenyellow'=>'ADFF2F',
            'grey'=>'808080',
            'honeydew'=>'F0FFF0',
            'hotpink'=>'FF69B4',
            'indianred'=>'CD5C5C',
            'indigo'=>'4B0082',
            'ivory'=>'FFFFF0',
            'khaki'=>'F0E68C',
            'lavender'=>'E6E6FA',
            'lavenderblush'=>'FFF0F5',
            'lawngreen'=>'7CFC00',
            'lemonchiffon'=>'FFFACD',
            'lightblue'=>'ADD8E6',
            'lightcoral'=>'F08080',
            'lightcyan'=>'E0FFFF',
            'lightgoldenrodyellow'=>'FAFAD2',
            'lightgray'=>'D3D3D3',
            'lightgreen'=>'90EE90',
            'lightgrey'=>'D3D3D3',
            'lightpink'=>'FFB6C1',
            'lightsalmon'=>'FFA07A',
            'lightseagreen'=>'20B2AA',
            'lightskyblue'=>'87CEFA',
            'lightslategray'=>'778899',
            'lightslategrey'=>'778899',
            'lightsteelblue'=>'B0C4DE',
            'lightyellow'=>'FFFFE0',
            'lime'=>'00FF00',
            'limegreen'=>'32CD32',
            'linen'=>'FAF0E6',
            'magenta'=>'FF00FF',
            'maroon'=>'800000',
            'mediumaquamarine'=>'66CDAA',
            'mediumblue'=>'0000CD',
            'mediumorchid'=>'BA55D3',
            'mediumpurple'=>'9370D0',
            'mediumseagreen'=>'3CB371',
            'mediumslateblue'=>'7B68EE',
            'mediumspringgreen'=>'00FA9A',
            'mediumturquoise'=>'48D1CC',
            'mediumvioletred'=>'C71585',
            'midnightblue'=>'191970',
            'mintcream'=>'F5FFFA',
            'mistyrose'=>'FFE4E1',
            'moccasin'=>'FFE4B5',
            'navajowhite'=>'FFDEAD',
            'navy'=>'000080',
            'oldlace'=>'FDF5E6',
            'olive'=>'808000',
            'olivedrab'=>'6B8E23',
            'orange'=>'FFA500',
            'orangered'=>'FF4500',
            'orchid'=>'DA70D6',
            'palegoldenrod'=>'EEE8AA',
            'palegreen'=>'98FB98',
            'paleturquoise'=>'AFEEEE',
            'palevioletred'=>'DB7093',
            'papayawhip'=>'FFEFD5',
            'peachpuff'=>'FFDAB9',
            'peru'=>'CD853F',
            'pink'=>'FFC0CB',
            'plum'=>'DDA0DD',
            'powderblue'=>'B0E0E6',
            'purple'=>'800080',
            'red'=>'FF0000',
            'rosybrown'=>'BC8F8F',
            'royalblue'=>'4169E1',
            'saddlebrown'=>'8B4513',
            'salmon'=>'FA8072',
            'sandybrown'=>'F4A460',
            'seagreen'=>'2E8B57',
            'seashell'=>'FFF5EE',
            'sienna'=>'A0522D',
            'silver'=>'C0C0C0',
            'skyblue'=>'87CEEB',
            'slateblue'=>'6A5ACD',
            'slategray'=>'708090',
            'slategrey'=>'708090',
            'snow'=>'FFFAFA',
            'springgreen'=>'00FF7F',
            'steelblue'=>'4682B4',
            'tan'=>'D2B48C',
            'teal'=>'008080',
            'thistle'=>'D8BFD8',
            'tomato'=>'FF6347',
            'turquoise'=>'40E0D0',
            'violet'=>'EE82EE',
            'wheat'=>'F5DEB3',
            'white'=>'FFFFFF',
            'whitesmoke'=>'F5F5F5',
            'yellow'=>'FFFF00',
            'yellowgreen'=>'9ACD32'
        ];

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

Read Also: How to Send Mail in PHP Laravel?

Step 4: Create Blade File

resources/views/index.blade.php

<!DOCTYPE html>
<html>
<head>
    <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">
</head>
<body>
    <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>
                    <div class="card-body">

                        <form action="{{ route('colorCode') }}" method="POST">
                            @csrf
                            <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">
                                
                                @error('name')
                                    <span class="text-danger">{{ $message }}</span>
                                @enderror

                                @if(isset($error))
                                    <span class="text-danger">{{ $error }}</span>
                                @endif
                            </div>
                            <div class="text-center mt-3">
                                <button type="submit" class="btn btn-primary">Submit</button>
                            </div>
                            </div>     
                        </form>

                        @if(isset($colorCode))
                            <div class="row my-3">
                                <div class="col-md-12 d-flex justify-content-center">
                                    <p>color code : <strong>#{{ $colorCode }}</strong></p>
                                </div>
                                <div class="col-md-12 d-flex justify-content-center">
                                        <div style="height: 100px; width: 100px; border: 1px solid black; background-color: #{{ $colorCode  }}; "></div>
                                </div>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
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:

http://localhost:8000/color-code

Read Also: Laravel 8 Sanctum API Authentication

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

close

You may also like