separate red green blue and alpha values from rgba string - php

I am separating Red, Green, Blue and Alpha values of rgba color values using php the code work fine when i have integer value but get only decimal part even have a floating values
this is the code
$myRGBString = "rgba(132,15,153,0.7)";
sscanf($myRGBString, 'rgba(%d,%d,%d,%d)', $red, $green, $blue, $alpha);
Above Code return aplha value 0 not 0.7 how can i get floating values of alpha

Use %f instead of %d for the alpha value.
C:\Users\Niet>php -r "sscanf('rgba(132,15,153,0.7)','rgba(%d,%d,%d,%f)',$r,$g,$b,$a); var_dump($r,$g,$b,$a);"
int(132)
int(15)
int(153)
float(0.7)

Related

unrecognized color `00000f' with Convert Command

I am getting the error while trying to run convert command. Here is the command.
convert -colorspace rgb 10338_1.ai -transparent 00000f 10338_1.png
and i am getting the error is
convert: unrecognized color `00000f' # color.c/GetColorInfo/965.
Any Solution?
The color should be given in HTML notation with hash in front of it:
convert -colorspace rgb 10338_1.ai -transparent '#00000f' 10338_1.png
The previous answer is correct about the hashtag. For more info see http://www.imagemagick.org/script/color.php.
We can analyse your image by looking at its histogram, like this:
convert a.ps -format "%c" histogram:info:
608007: ( 0, 0, 0, 0) #0000000000000000 cmyk(0,0,0,0)
58793: ( 0, 0, 0,65535) #000000000000FFFF cmyk(0,0,0,255)
6551: ( 0, 0, 0,34952) #0000000000008888 cmyk(0,0,0,136)
5095: ( 0, 0, 0,48059) #000000000000BBBB cmyk(0,0,0,187)
4350: ( 0, 0, 0,17476) #0000000000004444 cmyk(0,0,0,68)
3297: ( 0, 0, 0,61166) #000000000000EEEE cmyk(0,0,0,238)
2897: ( 0, 0, 0, 4369) #0000000000001111 cmyk(0,0,0,17)
First, you notice it is in CMYK colourspace, not RGB. You can see that the predominant colour is black with 608007 pixels, and that all the other colours in the image are actually just shades of black, but all have zero values for the CMY components. That means you lose nothing by just extracting the blacks into a greyscale image like this:
convert a.ps -channel K out.png
Maybe now you can do what you want, for example you can set the white pixels transparent like this:
convert out.png -transparent white result.png

use a range of colors from the value of RGB value

I want to use a range of colors using the value of RGB values
Suppose rbg of black color in ideal condition is rgb(0,0,0) but when i scan any image.It's color range varies from 0 to 51 like rgb(31,31,31),rgb(49,49,49) etc
what i required to use a color range for condition
if(r=0 to 51 && g=0 to 51 && b=0 to 51)
condition execute
Another thing all three values ie rgb should have similar value for example:-rgb(0,0,0) rgb(31,31,31)etc
so that combine color resembles black color
Making sense?
Im using PHP
You can transform the RGB color first to HSL, with HSL you can compare Hue, Saturation and Luminance of colors.
Comparing the Hue of two colors, gets all colors that are for example blue(ish). Black and White are a bit special in this case, you could test for extreme Luminance (near 0% or near 100%) for black/white, or low Saturation (near 0%) for grey colors.
Can't you just to some comparisons of the values to determine if they fall in the black spectrum of RGB? Such as:
$rgb = array("R" => 0, "G" => 0, "B" => 0);
if($rgb->R >= 0 && $rgb->R <= 51 && $rgb->G >= 0 && $rgb->G <= 51 && $rgb->B >= 0 && $rgb->B <= 51) {
// RGB value is in black spectrum
}

Calculating color combination with hex values

In additive color mixing primary colors are Red, Green and Blue (RGB).
Red = #ff0000
Green = #00ff00
Blue = #0000ff
Combining Red (#ff0000) and Green (#00ff00) makes Yellow (#ffff00)
Is there some formula to calulate the hex code of a color resulting from the combination of two others ?
Something like #ff0000 + #00ff00 when applied to such a formula gives #ffff00
You can add two HEX string like this in PHP:
$red = "FF0000";
$green = "00FF00";
$yellow = dechex(hexdec($red) + hexdec($green));
echo $yellow;
Live Demo
What that snippet is basically doing is converting the hex strings to numbers, adding them together, and then converting the sum back to a hex string.
Reference Links:
hexdec |
dechex

Converting int to RGB. Visualization in PHP and HTML/CSS

I'm trying to visualize a data on a grid with cell values actually represented by color. Red means high and blue means low. I was so naive in thinking that PHP's dechex() will help me by simply getting the hexadecimal equivalent of the int and using it as background-color in CSS (I did apply the necessary padding of zeros for small values).
But it doesn't quite get me what I want. Is there an algorithm that will let me visualize this properly? Red means high, blue means low.
My current code is this:
<?php
$dec = (int) $map[$y][$x]["total_score"];
$hex = dechex($dec);
$color = ($dec <= 65535) ? (($dec) ? "00$hex" : "ffffff") :
(($dec <= 1048575) ? ("0$hex") : $hex);
?>
Notice what it does:
ff0000 in decimal is smaller than ff00ff but on color, the first will show red and the latter violet. I want red to represent very high decimals and blue very low decimals.
I think RGB is not the very best color model here. I'd go with HSL - supported by modern browsers: color: hsl(0.5, 0.5, 0.5) and easily to convert to RGB.
HSL let's you define saturation and lightness of the color and the color itself quite easily. Blue is 240 deg, red is 360 deg so all you have to do is to map "low" to 240, "high" to 360 and all mid-value to 240-360 range.
Replace Red with Green and Remove Green by masking with 0xFF00FF in order to keep Red and Blue only.
$color = $your_decimal_number_you_want_to_colorize; // for example $color is 0x00F777
$color = $color << 8; // color is 0xF77700
$color = $color + ($color & 0x00FF00); // color is 0xF77777
$color = $color & 0xFF00FF; // color is 0xF70077
Using the code above, if $a > $b then $a will be more red than $b.

GD Color Replacement

This is killing me..
The Image has a background of solid red, a foreground of solid grey. On the edges of the foreground there is some anti-aliasing from grey to white.
What I need to do is change the foreground color to a user specified color and the red to transparent. This is easy enough using imagecolorcloset + imagecolorset/imagecolortransparent. The problem is the antialasing part.
I believe I need to loop through the pixels in the image. This would appear to work: http://www.php.net/manual/en/book.image.php#98153
I believe that I now need to determine the color difference between the foreground color and the anti aliased color.
So...
Foreground Color = rgb (153, 153, 153)
Anti-Alias Pixel = rgb (173, 173, 173)
New Color = rgb (0, 0, 255)
How do I determine the difference in color between the AA Pixel and the Foreground Color then apply it to the new color?
Edit: Turns out it's to do with factors
The anti-alias value should be the same factor so
<?
//old foreground colour
$of = array (220, 220, 220);
//old anti alias colour
$oaa = array (110, 110, 110);
//factors
$factors = array ($of[0]/$oaa[0], $of[1]/$oaa[1], $of[2]/$oaa[2]);
//new foreground
$nf = array (156, 34, 34);
//new anti alias
$naa = array($nf[0]/$factors[0], $nf[1]/$factors[1], $nf[2]/$factors[2]);
?>

Categories