Last active
June 25, 2018 11:08
-
-
Save Apeli/e3f3be773dc2d6da500bb2d0d2b28b06 to your computer and use it in GitHub Desktop.
Couldn't find a ncs to hex (or ncs to anything) color converter in PHP, so I translated the convert function from https://github.com/m90/ncs-color to PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Eats NCS S 3010-G20Y and puts out #a8bca0 | |
function ncs2hex($ncs) { | |
$ncs = trim($ncs); | |
$ncs = strtoupper($ncs); | |
$ncs = preg_match("/^(?:NCS|NCS\sS)\s(\d{2})(\d{2})-(N|R|G|B|Y)(\d{2})?(R|G|B|Y)?$/", $ncs, $output_array); | |
$ncs = $output_array; | |
$Sn = intval($ncs[1]); | |
$Cn = intval($ncs[2]); | |
$C1 = $ncs[3]; | |
$N = intval($ncs[4]); | |
$Ra = null; | |
$x1 = null; | |
$Ba = null; | |
$x2 = null; | |
$x3 = null; | |
$x5 = null; | |
$Ga = null; | |
$x6 = null; | |
$x7 = null; | |
$x8 = null; | |
$Rc = null; | |
$Gc = null; | |
$Bc = null; | |
$top = null; | |
$ss = null; | |
if ($C1 !== 'N') { | |
$S = (1.05 * $Sn - 5.25); | |
$C = $Cn; | |
$Cx = $Cn < 10 ? '0'+$Cn : $Cn; | |
// extract red | |
if ($C1 === 'Y' && $N <= 60) { | |
$Ra = 1; | |
} else if (($C1 === 'Y' && $N > 60) || ($C1 === 'R' && $N <= 80)) { | |
if ($C1 === 'Y') { | |
$x1 = $N - 60; | |
} else { | |
$x1 = $N + 40; | |
} | |
$Ra = ((sqrt(14884 - pow($x1, 2))) - 22) / 100; | |
} else if (($C1 === 'R' && $N > 80) || ($C1 === 'B')) { | |
$Ra = 0; | |
} else if ($C1 === 'G') { | |
$x1 = ($N - 170); | |
$Ra = ((sqrt(33800 - pow($x1, 2))) - 70) / 100; | |
} | |
// ext$ract blue | |
if ($C1 === 'Y' && $N <= 80) { | |
$Ba = 0; | |
} else if (($C1 === 'Y' && $N > 80) || ($C1 === 'R' && $N <= 60)) { | |
if ($C1 === 'Y') { | |
$x2 = ($N - 80) + 20.5; | |
} else { | |
$x2 = ($N + 20) + 20.5; | |
} | |
$Ba = (104 - (sqrt(11236 - pow($x2, 2)))) / 100; | |
} else if (($C1 === 'R' && $N > 60) || ($C1 === 'B' && $N <= 80)) { | |
if ($C1 === 'R') { | |
$x3 = ($N - 60) - 60; | |
} else { | |
$x3 = ($N + 40) - 60; | |
} | |
$Ba = ((sqrt(10000 - pow($x3, 2))) - 10) / 100; | |
} else if (($C1 === 'B' && $N > 80) || ($C1 === 'G' && $N <= 40)) { | |
if ($C1 === 'B') { | |
$x5 = ($N - 80) - 131; | |
} else { | |
$x5 = ($N + 20) - 131; | |
} | |
$Ba = (122 - (sqrt(19881 - pow($x5, 2)))) / 100; | |
} else if ($C1 === 'G' && $N > 40) { | |
$Ba = 0; | |
} | |
// exct$ract green | |
if ($C1 === 'Y') { | |
$Ga = (85 - 17 / 20 * $N) / 100; | |
} else if ($C1 === 'R' && $N <= 60) { | |
$Ga = 0; | |
} else if ($C1 === 'R' && $N > 60) { | |
$x6 = ($N - 60) + 35; | |
$Ga = (67.5 - (sqrt(5776 - pow($x6, 2)))) / 100; | |
} else if ($C1 === 'B' && $N <= 60) { | |
$x8 = (1 * $N - 68.5); | |
$Ga = (6.5 + (sqrt(7044.5 - pow($x8, 2)))) / 100; | |
} else if (($C1 === 'B' && $N > 60) || ($C1 === 'G' && $N <= 60)) { | |
$Ga = 0.9; | |
} else if ($C1 === 'G' && $N > 60) { | |
$x7 = ($N - 60); | |
$Ga = (90 - (1 / 8 * x7)) / 100; | |
} | |
// ext$ract satu$ration | |
$x2 = ($Ra + $Ga + $Ba) / 3; | |
$Rc = (($x2 - $Ra) * (100 - $C) / 100) + $Ra; | |
$Gc = (($x2 - $Ga) * (100 - $C) / 100) + $Ga; | |
$Bc = (($x2 - $Ba) * (100 - $C) / 100) + $Ba; | |
// ext$ract blackness | |
if ($Rc > $Gc && $Rc > $Bc) { | |
$top = $Rc; | |
} else if ($Gc > $Rc && $Gc > $Bc) { | |
$top = $Gc; | |
} else if ($Bc > $Rc && $Bc > $Gc) { | |
$top = $Bc; | |
} else { | |
$top = ($Rc + $Gc + $Bc) / 3; | |
} | |
$ss = 1 / $top; | |
$rgb = [ | |
"R" => intval(($Rc * $ss * (100 - $S) / 100) * 255), | |
"G" => intval(($Gc * $ss * (100 - $S) / 100) * 255), | |
"B" => intval(($Bc * $ss * (100 - $S) / 100) * 255), | |
]; | |
} else { | |
$v = intval((1 - $Sn / 100) * 255); | |
$rgb = [ | |
"R" => $v, | |
"G" => $v, | |
"B" => $v, | |
]; | |
} | |
return sprintf("#%02x%02x%02x", $rgb['R'], $rgb['G'], $rgb['B']); // #0d00ff | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment