John Davidson

I want to use a javascript code in my PHP API

0 comments
Message:


I am using a javascript code in my PHP API but when i am calling that API from android app or POSTMAN the JS doesn't get executed it just gets printed so is there any way to use it with PHP or can anyone convert my JS code to a PHP code that i can use in my API


here is the JS library -


    var ntc = {

init: function() {
var color, rgb, hsl;
for(var i = 0; i < ntc.names.length; i++)
{
color = "#" + ntc.names[i][0];
rgb = ntc.rgb(color);
hsl = ntc.hsl(color);
ntc.names[i].push(rgb[0], rgb[1], rgb[2], hsl[0], hsl[1], hsl[2]);
}
},

name: function(color) {

color = color.toUpperCase();
if(color.length < 3 || color.length > 7)
return ["#000000", "Invalid Color: " + color, false];
if(color.length % 3 == 0)
color = "#" + color;
if(color.length == 4)
color = "#" + color.substr(1, 1) + color.substr(1, 1) + color.substr(2, 1) + color.substr(2, 1) + color.substr(3, 1) + color.substr(3, 1);

var rgb = ntc.rgb(color);
var r = rgb[0], g = rgb[1], b = rgb[2];
var hsl = ntc.hsl(color);
var h = hsl[0], s = hsl[1], l = hsl[2];
var ndf1 = 0; ndf2 = 0; ndf = 0;
var cl = -1, df = -1;

for(var i = 0; i < ntc.names.length; i++)
{
if(color == "#" + ntc.names[i][0])
return ["#" + ntc.names[i][0], ntc.names[i][1], true];

ndf1 = Math.pow(r - ntc.names[i][2], 2) + Math.pow(g - ntc.names[i][3], 2) + Math.pow(b - ntc.names[i][4], 2);
ndf2 = Math.pow(h - ntc.names[i][5], 2) + Math.pow(s - ntc.names[i][6], 2) + Math.pow(l - ntc.names[i][7], 2);
ndf = ndf1 + ndf2 * 2;
if(df < 0 || df > ndf)
{
df = ndf;
cl = i;
}
}

return (cl < 0 ? ["#000000", "Invalid Color: " + color, false] : ["#" + ntc.names[cl][0], ntc.names[cl][1], false]);
},

// adopted from: Farbtastic 1.2
// http://acko.net/dev/farbtastic
hsl: function (color) {

var rgb = [parseInt('0x' + color.substring(1, 3)) / 255, parseInt('0x' + color.substring(3, 5)) / 255, parseInt('0x' + color.substring(5, 7)) / 255];
var min, max, delta, h, s, l;
var r = rgb[0], g = rgb[1], b = rgb[2];

min = Math.min(r, Math.min(g, b));
max = Math.max(r, Math.max(g, b));
delta = max - min;
l = (min + max) / 2;

s = 0;
if(l > 0 && l < 1)
s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));

h = 0;
if(delta > 0)
{
if (max == r && max != g) h += (g - b) / delta;
if (max == g && max != b) h += (2 + (b - r) / delta);
if (max == b && max != r) h += (4 + (r - g) / delta);
h /= 6;
}
return [parseInt(h * 255), parseInt(s * 255), parseInt(l * 255)];
},

// adopted from: Farbtastic 1.2
// http://acko.net/dev/farbtastic
rgb: function(color) {
return [parseInt('0x' + color.substring(1, 3)), parseInt('0x' + color.substring(3, 5)), parseInt('0x' + color.substring(5, 7))];
},

names: [
["000000", "black"],
["070707", "black"],
["0F0F0F", "black"],
["121111", "black"],
["181414", "black"],
["100C0C", "black"],
["1B1A1A", "black"],
["0A0707", "black"],
["0B0101", "black"],
["0A0505", "black"],
["F0F8FF", "blue"],
["1C34DB", "blue"],
["32CD32", "green"],
["41DC2D", "green"],
["1CDC03", "green"],
["32CD32", "green"],
["FFFAF0", "white"],
["FFFFFF", "white"],
["F7F5F5", "white"],
["FCFCFC", "white"],
["FEF7F7", "white"],
["880808", "red"],
["AA4A44", "red"],
["800020", "red"],
["FF0000", "red"],
["CD5C5C", "red"],
["800000", "pink"],
["FC07EA", "pink"],
["F520E5", "pink"],
["BA08AD", "pink"],
["CB25BF", "pink"],
["F719E7", "pink"],
["663399", "purple"],
["AF00FC", "purple"],
["B825F9", "purple"],
["C13FFA", "purple"],
["A534D6", "purple"],
["A109E3", "purple"],
["D57EFB", "purple"],
["FFFFE0", "yellow"],
["F7E90B", "yellow"],
["F7EB29", "yellow"],
["E7DB1B", "yellow"],
["C5BB12", "yellow"]
]

}

ntc.init();

this library is used to get a closest hex color name for the hex color passed to it from the array in it.


this is how i am using it in my php file -


<!-- Include name that color library -->
<script type="text/javascript" src="color.js"></script>

<script type="text/javascript">
// 1. You need a hex code of the color
var ColorCode = "<?=$_REQUEST['hex_color']?>";

// 2. Rate the color using NTC
var ntcMatch = ntc.name(ColorCode);

// 3. Handle the result. The library returns an array with the identified color.

// 3.A RGB value of closest match e.g #01826B
console.log(ntcMatch[0]);
// Text string: Color name e.g "Deep Sea"
console.log(ntcMatch[1]);
document.write(ntcMatch[1]);
// True if exact color match, a Boolean
console.log(ntcMatch[2]);
</script>

the result i get when i call it from postman or android app -



the result i get in browser and i need in postman -




My Answer:
To use the functionality of the JavaScript code in your PHP API, you can convert the JavaScript code to PHP code. Here is the converted PHP code that you can use in your API:

php

$ntc = array(
'names' => array(
array("000000", "black"),
array("070707", "black"),
array("0F0F0F", "black"),
array("121111", "black"),
array("181414", "black"),
array("100C0C", "black"),
array("1B1A1A", "black"),
array("0A0707", "black"),
array("0B0101", "black"),
array("0A0505", "black"),
array("F0F8FF", "blue"),
array("1C34DB", "blue"),
array("32CD32", "green"),
array("41DC2D", "green"),
array("1CDC03", "green"),
array("32CD32", "green"),
array("FFFAF0", "white"),
array("FFFFFF", "white"),
array("F7F5F5", "white"),
array("FCFCFC", "white"),
array("FEF7F7", "white"),
array("880808", "red"),
array("AA4A44", "red"),
array("800020", "red"),
array("FF0000", "red"),
array("CD5C5C", "red"),
array("800000", "pink"),
array("FC07EA", "pink"),
array("F520E5", "pink"),
array("BA08AD", "pink"),
array("CB25BF", "pink"),
array("F719E7", "pink"),
array("663399", "purple"),
array("AF00FC", "purple"),
array("B825F9", "purple"),
array("C13FFA", "purple"),
array("A534D6", "purple"),
array("A109E3", "purple"),
array("D57EFB", "purple"),
array("FFFFE0", "yellow"),
array("F7E90B", "yellow"),
array("F7EB29", "yellow"),
array("E7DB1B", "yellow"),
array("C5BB12", "yellow")
)
);

function rgb($color) {
return array(hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
}

function hsl($color) {
$rgb = rgb($color);
$r = $rgb[0] / 255;
$g = $rgb[1] / 255;
$b = $rgb[2] / 255;

$min = min($r, min($g, $b));
$max = max($r, max($g, $b));
$delta = $max - $min;
$l = ($min + $max) / 2;

$s = 0;
if ($l > 0 && $l < 1) {
$s = $delta / ($l < 0.5 ? (2 * $l) : (2 - 2 * $l));
}

$h = 0;
if ($delta > 0) {
if ($max == $r && $max != $g) $h += ($g - $b) / $delta;
if ($max == $g && $max != $b) $h += (2 + ($b - $r) / $delta);
if ($max == $b && $max != $r) $h += (4 + ($r - $g) / $delta);
$h /= 6;
}

return array(intval($h * 255), intval($s * 255), intval($l * 255));
}

function name($color) {
global $ntc;

$color = strtoupper($color);
if (strlen($color) < 3 || strlen($color) > 7) {
return array("#000000", "Invalid Color: " . $color, false);
}
if (strlen($color) % 3 == 0) {
$color = "#" . $color;
}
if (strlen($color) == 4) {
$color = "#" . $color[1] . $color[1] . $color[2] . $color[2] . $color[3] . $color[3];
}

$rgb = rgb($color);
$r = $rgb[0];
$g = $rgb[1];
$b = $rgb[2];
$hsl = hsl($color);
$h = $hsl[0];
$s = $hsl[1];
$l = $hsl[2];
$ndf1 = 0;
$ndf2 = 0;
$ndf = 0;
$cl = -1;
$df = -1;

foreach ($ntc['names'] as $key => $value) {
if ($color == "#" . $value[0]) {
return array("#" . $value[0], $value[1], true);
}

$ndf1 = pow($r - $value[2], 2) + pow($g - $value[3], 2) + pow($b - $value[4], 2);
$ndf2 = pow($h - $value[5], 2) + pow($s - $value[6], 2) + pow($l - $value[7], 2);
$ndf = $ndf1 + $ndf2 * 2;
if ($df < 0 || $df > $ndf) {
$df = $ndf;
$cl = $key;
}
}

return ($cl < 0 ? array("#000000", "Invalid Color: " . $color, false) : array("#" . $ntc['names'][$cl][0], $ntc['names'][$cl][1], false));
}

// 1. You need a hex code of the color
$ColorCode = $_REQUEST['hex_color'];

// 2. Rate the color using NTC
$ntcMatch = name($ColorCode);

// 3. Handle the result. The library returns an array with the identified color.

// 3.A RGB value of closest match e.g #01826B
echo $ntcMatch[0];
// Text string: Color name e.g "Deep Sea"
echo $ntcMatch[1];
// True if exact color match, a Boolean
echo $ntcMatch[2];

?>


You can now use this PHP code in your API to get the closest hex color name for the hex color passed to it.

Rate this post

3 of 5 based on 3699 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us