<?phpnamespaceMayMeow\PHPColor;classColor{/** int $red */public$red;/** int $green */public$green;/** int $blue */public$blue;publicfunction__construct($red,$green,$blue){$this->red=$red;$this->green=$green;$this->blue=$blue;}/**
* Method convertToHex
* Covert given color to string #RRGGBB
*/publicstaticfunctionconvertToHex(Color$color){$red=dechex($color->red);if(strlen($red)<2)$red='0'.$red;// check string length and add zero if needed$green=dechex($color->green);if(strlen($green)<2)$green='0'.$green;$blue=dechex($color->blue);if(strlen($blue)<2)$blue='0'.$blue;return'#'.$red.$green.$blue;}/**
* Method convertToRGB
* Convert given color string back to RGB color values
*/publicstaticfunctionconvertToRGB($hex){$hex=ltrim($hex,"#");$red=hexdec(substr($hex,0,2));$green=hexdec(substr($hex,2,2));$blue=hexdec(substr($hex,4,2));returnnewColor($red,$green,$blue);}}
Time flies while you're having fun, but then one day your bio says something about being a 30+ year veteran in software engineering. Still, I've not seen it all, let alone done it all (yet).
// round the number to the closest value (255 or 0)functionroundToClosest($color){if($color>255){return255;}elseif($color<0){return0;}return$color;}functionrgb($r,$g,$b){$r=roundToClosest($r);$g=roundToClosest($g);$b=roundToClosest($b);returnsprintf("%02X%02X%02X",$r,$g,$b);}
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
Thanks for the tip! I've mostly used string formating for dealing with point numbers in decimals. Need to explore more use cases because the syntax is indeed preferable.
We're a place where coders share, stay up-to-date and grow their careers.
My JavaScript solution, which would look a lot nicer if the TC39 finally added a pipeline operator.
String.prototype.padStart would handle the
addZeroestrick for you. ;)I got really confused when the link opened in Portuguese. I had to check my VPN 😆
Whoops! Sorry about that. :P Should be fixed now.
We have
padStartnow in strings to add characters to the beginning up to a certain length.I've modified it to use
padStartinstead of my custom function.Thanks @lffg and @avalander !
Nodejs (not web):
Not necessarily actually efficient, but definitely hilarious.
General JS:
Elm
Test.
Haskell, using the word8 type to ensure the inputs are from 0-255.
My solution in PHP
Python solution, supports 1 or 3 arguments like CSS.
In C#, you don't even really need to implement a method for this, as its easily done with string interpolation;
(Useful for unique ad-hoc strings, but usually better factored out to a method anyway)
My PHP Solution
Ruby solution
In Go.
I'd do this with Uint8Array
Thanks for the tip! I've mostly used string formating for dealing with point numbers in decimals. Need to explore more use cases because the syntax is indeed preferable.