PHP Random Color select - php

I am writing randomColorSelect() function, so that when the script is executed, prints the word RED 10% of the time, the word BLUE 50% of the time and the word GREEN 80% of the time: but not getting how to get this as i am new in PHP.

http://php.net/manual/en/function.rand.php
This should give the desired effect.
$red_dice = rand(0, 100); // 0% to 100%
$blue_dice = rand(0, 100); // 0% to 100%
$green_dice = rand(0, 100); // 0% to 100%
if ($red_dice <= 10)
{
echo "RED\n";
}
if ($blue_dice <= 50)
{
echo "BLUE\n";
}
if ($green_dice <= 80)
{
echo "GREEN\n";
}

Building on the believe that the percentages should add up to 100% total:
$number = rand(1, 100);
// 10%
if($number <= 10)
{
echo 'RED';
}
// 50%
else if(($number >= 11) && ($number <= 60))
{
echo 'BLUE';
}
// rest-%, in this case 30
else
{
echo 'GREEN';
}

Related

Short Number format in PHP Indian Based

Googled for Short Number Format in PHP, got thousands of working results from StackOverflow, Github, etc.. but I didn't get any results for Indian Format.
Eg:
In other countries: 1000 is as 1k, 100000 is as 100k, 10000000 is as 10m
But in India: 1000 is as 1k OR 1T, 100000 is as 1L, 10000000 is as 1c
Can anyone help me to do that?
With the help of this answer
I came up with a solution for you:
<?php
function indian_short_number($n) {
if($n <= 99999){
$precision = 3;
if ($n < 1000) {
// Anything less than a thousand
$n_format = number_format($n);
} else {
// At least a thousand
$n_format = number_format($n / 1000, $precision) . ' K';
}
/* Use this code if you want to round off your results
$n_format = ceil($n_format / 10) * 10;
if($n >= 1000){
$n_format = $n_format . ' K';
}
*/
return $n_format;
}
else{
$precision = 2;
if ($n > 99999 && $n < 9999999) {
// Anything more than a Lac and less than crore
$n_format = number_format($n / 100000, $precision) . ' L';
} elseif ($n > 999999) {
// At least a crore
$n_format = number_format($n / 10000000, $precision) . ' C';
}
/* Use this code if you want to round off your results
$n_format = ceil($n_format / 10) * 10;
if($n >= 10000 && $n < 10000000){
$n_format = $n_format . ' L';
}
elseif($n >= 1000000){
$n_format = $n_format . ' C';
}
*/
return $n_format;
}
}
echo indian_short_number(10000);
?>
The code for rounding off is not proper. (For 18100 it rounds to 20 K instead of 19 K)
I will be thankful if any of the visitors edit the answer with the fixing it.
Hope it helps you.

Can this be done with a percentage instead of static values

Alright guys, Basically i've got some code which is working from an if statement, I'm wondering if it's possible to recode it to work from a percentage, here's the current code
#sort($prices_array);
if ($prices_array[1] > 10000 AND $prices_array[1] < 50000) {
$sell_at = $prices_array[1] - 250;
} elseif ($prices_array[1] > 50000 AND $prices_array[1] < 100000) {
$sell_at = $prices_array[1] - 500;
} else {
$sell_at = $prices_array[1] - 100;
}
$buy_at = $prices_array[1] - 1000;
if ($prices_array[1] >= 10000) {
$buy_at = $buy_at - 1000;
if ($prices_array[1] >= 20000) {
$buy_at = $buy_at - 1000;
if ($prices_array[1] >= 30000) {
$buy_at = $buy_at - 1000;
}
if ($prices_array[1] >= 40000) {
$buy_at = $buy_at - 1000;
}
if ($prices_array[1] >= 50000) {
$buy_at = $buy_at - 750;
}
if ($prices_array[1] >= 60000) {
$buy_at = $buy_at - 500;
}
if ($prices_array[1] >= 70000) {
$buy_at = $buy_at - 500;
}
}
}
I know the code is quite horribly done, but would it be possible to base the $buy_at from an certain percentage for example such as 5% lower than the sell_at but rounded to the correct decrements from the if statement
First of all, I think a switch would be nicer form here. Secondly, you can use this function to set up rounding to whatever increments you want:
function roundUpTo($number, $increments) {
$increments = 1 / $increments;
return (ceil($number * $increments) / $increments);
}
To find a percentage of a value, just multiply the value by the percentage divided by 100.

Strange behaviour of mt_rand used for random plotting

That's the code which generates the x-marks:
for ($i = 0; $i < $maxdata; $i++)
{
$xcoord = mt_rand(50, 450);
$ycoord = mt_rand(50, 450);
imagechar($im, 5, $xcoord, $ycoord, $string, $black);
if (($xcoord < 50) || ($xcoord > 450) || ($ycoord < 50) || ($ycoord > 450))
{
$p_bad += 1;
}
}
But this happens (got bad marks and at y-coord):
http://imgur.com/2TZXfY1
Rly don't know where is the problem, image size is 500 x 500 pixels, x-coord is from 50 to 450 pixel, and y-coord is from 50 to 450 pixel too ...

Which X11 color range does this color lie in

I'm building a tool where i pick the dominant colors from an image and i have done that bit pretty well, the problem i'm facing now is how to combine the returned colors into a broad palette range like the X11 color range http://en.wikipedia.org/wiki/Web_colors#X11_color_names
So for example if i got : Color RGB:rgb(102,102,153), i would want to chalk it up to the purple colors and rgb(51,102,204) to blue and so on and so forth. Now i really can't figure out how to do this. Is there a library or something i can use or how should i code it? I'm using imagemagick and php btw.
Is it possible to generate an array containing the range of rgb for each base colors and then see if my new color lies in it?
Thanks in advance guys!
I've been looking for exactly the same thing for an upcoming project, though I'm happy with just the basic rainbow colours.
Searching around, the best way seems to be to convert your RGB colour to HSL. The (H)ue part is very useful to see what broad part of the rainbow the colour lies in. Then add a couple of extra bits to capture black, white, grey and brown.
Here is my PHP code with a reference to the RGB -> HSL convert routine. I'm sure it could be optimized a bit better but it's a start!
Obviously colour is quite subjective so you may want to play around with the values for each colour range - just use one of the online colour picker utilities or even the Windows colour picker.
<?php
function RGB_TO_HSV ($R, $G, $B) { // RGB Values:Number 0-255
// HSV Results:Number 0-1
// this function from http://stackoverflow.com/questions/1773698/rgb-to-hsv-in-php
$HSL = array();
$var_R = ($R / 255);
$var_G = ($G / 255);
$var_B = ($B / 255);
$var_Min = min($var_R, $var_G, $var_B);
$var_Max = max($var_R, $var_G, $var_B);
$del_Max = $var_Max - $var_Min;
$V = $var_Max;
if ($del_Max == 0) {
$H = 0;
$S = 0;
}
else {
$S = $del_Max / $var_Max;
$del_R = ( ( ( $max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_G = ( ( ( $max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_B = ( ( ( $max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
if ($var_R == $var_Max) $H = $del_B - $del_G;
else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;
if (H<0) $H++;
if (H>1) $H--;
}
$HSL['H'] = $H;
$HSL['S'] = $S;
$HSL['V'] = $V;
return $HSL;
}
// convert an RGB colour to HSL
$hsl = RGB_TO_HSV(51,102,204); // rgb values 0-255
$hue = round($hsl['H'] * 255, 0); // round hue from 0 to 255 for ease of use
$sat = $hsl['S']; // 0 to 1
$val = $hsl['V']; // 0 to 1
$colour = "Red"; // default to red
if ($hue >= 10 && $hue <= 35) {
$colour = "Orange";
if ($val < 0.69) $colour = "Brown";
}
if ($hue >= 36 && $hue <= 44) $colour = "Yellow";
if ($hue >= 45 && $hue <= 107) $colour = "Green";
if ($hue >= 108 && $hue <= 182) $colour = "Blue";
if ($hue >= 183 && $hue <= 206) $colour = "Purple";
if ($hue >= 207 && $hue <= 245) $colour = "Pink";
if ($val < 0.1) $colour = "Black";
if ($val > 0.9) $colour = "White";
if ($sat < 0.105) $colour = "Grey";
// show the result
echo $colour;
?>
OK, forget the previous code, here is a much more reliable version I put together from several sources which also uses the more common 0-360 for hue.
I also added a test little routine that picks random colours then 'tells' you what they are!
Seems more reliable, some fiddling might be needed around the cut-off values (especially orange/grey etc). I used this online colour picker for testing.
<?php
function RGBtoHSL($r,$g,$b) {
// based on code and formulas from:
// http://www.had2know.com/technology/hsl-rgb-color-converter.html
// http://colorgrader.net/index.php/dictionary-a-tutorials/color-theory/93-math-behind-colorspace-conversions-rgb-hsl.html
// http://proto.layer51.com/d.aspx?f=1135
$max = max($r, $g, $b);
$min = min($r, $g, $b);
$d = ($max - $min) / 255;
$lum = round((($max+$min)/2)/255, 2);
$sat = 0;
if ($lum > 0) $sat = round($d/(1 - (2*$lum-1)), 2);
$hue = 0;
if(($r==$g) && ($g==$b)) $hue = 0;
else if($r>=$g && $g>=$b) $hue = 60*($g-$b)/($r-$b);
else if($g>=$r && $r>=$b) $hue = 60 + 60*($g-$r)/($g-$b);
else if($g>=$b && $b>=$r) $hue = 120 + 60*($b-$r)/($g-$r);
else if($b>=$g && $g>=$r) $hue = 180 + 60*($b-$g)/($b-$r);
else if($b>=$r && $r>=$g) $hue = 240 + 60*($r-$g)/($b-$g);
else if($r>=$b && $b>=$g) $hue = 300 + 60*($r-$b)/($r-$g);
else $hue = 0;
$hue = round($hue, 0);
$hsl = array();
$hsl['h'] = $hue;
$hsl['s'] = $sat;
$hsl['l'] = $lum;
return $hsl;
}
// example: pick 42 random colours then identify them
echo "<div style='float: left; width: 1000px;'>";
srand;
for ($f=0; $f < 42; $f++) {
$red = rand(0, 255);
$green = rand(0, 255);
$blue = rand(0, 255);
$hsl = RGBtoHSL($red, $green, $blue);
$hue = $hsl['h'];
$sat = $hsl['s'];
$lum = $hsl['l'];
$colour = "Red"; // default to red
if ($hue >= 11 && $hue <= 45) {
$colour = "Orange";
if ($lum < 0.51) $colour = "Brown";
}
if ($hue >= 46 && $hue <= 62) $colour = "Yellow";
if ($hue >= 63 && $hue <= 160) $colour = "Green";
if ($hue >= 161 && $hue <= 262) $colour = "Blue";
if ($hue >= 263 && $hue <= 292) $colour = "Purple";
if ($hue >= 293 && $hue <= 349) $colour = "Pink";
if ($sat < 0.07) $colour = "Grey"; // do grey before black/white
if ($lum < 0.10) $colour = "Black";
if ($lum > 0.97) $colour = "White";
echo "<div style='float: left; width: 150px; border: 1px solid #000000; margin: 5px;'>";
echo "<div style='float: left; width: 20px; height: 120px; background-color: rgb($red, $green, $blue); margin-right: 10px;'></div>";
echo "<p style='width: 33%; float: left;'>R=$red<br/>G=$green<br/>B=$blue</p>";
echo "<p style='width: 33%; float: right;'>H=$hue<br/>S=$sat<br/>L=$lum</p>";
echo "<p><b>$colour</b></p>";
echo "</div>";
}
echo "</div>";
?>

How to check pixels near specified pixel in php: color

Here i try to check image, but it has some "ellipses" of another color, and i try to smooth them by my color. But my code searches only for specific color, how to say him that for example 5% of difference is bad to?
How to find near my pixel all the same pixel and color them in another color?
<?php
function LoadJpeg($imgname)
{
$count = 0;
/* Attempt to open */
$im = #imagecreatefrompng($imgname);
$imagedata = getimagesize($imgname);
for($i=0; $i<$imagedata[0]; $i++){
for($j=0; $j<$imagedata[1]; $j++){
$rgb[$i][$j] = imagecolorat($im, $i, $j);
//echo $rgb[$i][$j];
//echo "<br>";
}
}
for($i=0; $i<$imagedata[0]-5; $i++){
for($j=0; $j<$imagedata[1]-5; $j++){
if (($rgb[$i][$j] == $rgb[$i+3][$j]) || ($rgb[$i][$j] == $rgb[$i][$j+3]))
{
#echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa";
$count = $count + 1;
//echo "<br> <br>";
//echo $count;
//$red = imagecolorallocate($im, 255, 255, 255);
imagesetpixel($im, $i, $j, 13437229);
}
}
}
return $im;
}
header('Content-Type: image/jpeg');
$img = LoadJpeg('1.png');
// echo "Image width is: " . $imagedata[0];
// echo "Image height is: " . $imagedata[1];
imagejpeg($img,null, 100);
?>
Main trouble is with strong ==, but some difference of this color must be catched by my if case.
What about calculating distance between each color component? If one of the components differ by more than 5 percents, return false:
// Returns RGB components of the color represented by an integer
function components($color) {
return array(($color >> 16) & 0xFF, ($color >> 8) & 0xFF, $color & 0xFF);
}
// Performs "similarity test" of 2 colors
function isSimilar($color1, $color2) {
$c1 = components($color1);
$c2 = components($color2);
for ($i = 0; $i < 3; $i++) {
$k = ($c1[$i] > $c2[$i]) ? ($c1[$i] - $c2[$i]) / $c2[$i] : ($c2[$i] - $c1[$i]) / $c1[$i];
if ($k > 0.05) return false;
}
return true;
}
// ...
if (isSimilar($rgb[$i][$j], $rgb[$i][$j + 3]) or isSimilar($rgb[$i][$j], $rgb[$i + 3][$j])) {
// ...
}
The code may require additional testing and tweaking, but I think you've got an idea.

Categories