DEV Community

Cover image for First package on packagist
anthowd
anthowd

Posted on

First package on packagist

Dear dev.to Readers,

Before diving into the details, I'd like to ask for a bit of leniency. As a newcomer, this marks my first time on this platform and also my inaugural creation posted on Packagist. I'm eager to share with you what I hope will be an enlightening experience.

My name is anthowd, French guy, and i'm a Laravel and PHP fan, while i like Javascript and some other languages too !

A while back, during a client project, I realized the challenge of finding a modern and straightforward tool for color conversions across different formats, such as Hexa, Ral, and RGB. After some exploration, it became apparent that there were few recent projects catering to this need. That's when I decided to take matters into my own hands.

My journey began with an exploration of available resources. Eventually, I stumbled upon an old GitHub repository containing a PHP table with Ral to Hexa color conversions. It served as a good starting point.

[
    [
        'number' => 'RAL1000',
        'rangeindex' => '1',
        'nameEnglish' => 'Green beige',
        'nameGerman' => 'Grünbeige',
        'hex' => 'CDBA88',
    ],
    [
        'number' => 'RAL1001',
        'rangeindex' => '1',
        'nameEnglish' => 'Beige',
        'nameGerman' => 'Beige',
        'hex' => 'D0B084',
    ],
    [
        'number' => 'RAL1002',
        'rangeindex' => '1',
        'nameEnglish' => 'Sand yellow',
        'nameGerman' => 'Sandgelb',
        'hex' => 'D2AA6D',
    ],
    // ...
]
Enter fullscreen mode Exit fullscreen mode

Subsequently, I developed a PHP class named ColorConverter, housing the necessary methods for executing these conversions efficiently and accurately. Drawing from the principles of object-oriented programming (OOP), I ensured this class was flexible, extensible, and, most importantly, developer-friendly.

namespace ColorConverter;

class ColorConverter
{
    public static function hexToRgb($hex)
    {
        $hex = str_replace('#', '', $hex);
        $r = hexdec(substr($hex, 0, 2));
        $g = hexdec(substr($hex, 2, 2));
        $b = hexdec(substr($hex, 4, 2));
        return ['R' => $r, 'G' => $g, 'B' => $b];
    }

    public static function rgbToHex($r, $g, $b)
    {
        $hex = sprintf("#%02x%02x%02x", $r, $g, $b);
        return $hex;
    }

    private function ralToRgb($ral)
    {
        $ralToRgbMap = include __DIR__ . '/sources/ral_to_hex.php';
        foreach ($ralToRgbMap as $color) {
            if ($color['number'] === $ral) {
                $rgb = self::hexToRgb($color['hex']);
                return $rgb;
            }
        }
        return null;
    }

// ...
Enter fullscreen mode Exit fullscreen mode

Here's an example of using my class to convert a Ral color to Hexa format:

use ColorConverter\ColorConverter;

require_once("./vendor/autoload.php");

$rgb = ColorConverter::ralToRgbMetas("RAL6003");
$hex = ColorConverter::rgbToHex(...array_values($rgb));
var_dump($hex); // output string(7) "#4b573e"
Enter fullscreen mode Exit fullscreen mode

This package represents a significant milestone in my development journey. To make it accessible to the community, I decided to publish it on Packagist, the leading platform for PHP package distribution.

After registering on Packagist, I linked my GitHub project, accessible here, to the package I created. This allowed for the availability of a stable version of my color converter and simplified its installation for developers via Composer.

As I progressed, I learned the importance of good development practices, particularly in version management. Creating releases on GitHub enabled users to install specific versions according to their needs and requirements.

In conclusion, this experience marks a significant step in my PHP development journey. Creating my first package and sharing it on Packagist has been a rewarding adventure, and I hope it proves useful to other developers in their projects.

Feel free to try out my package and share your feedback. Thank you in advance for your understanding and support!

Top comments (0)