[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Specifying rgba colors as variables

Joey@still_Learning.invalid

5/22/2014 12:17:00 AM

I'm trying to specify a group of colors as RGBa, but I'm lost.

Example code:

highlight_color = new Array(255,0,0);

for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {

gray_avg = ctx.getImageData(x, y, 1, 1).data[0];

if (gray_avg >= threshold) { gray = highlight_color; }
.....
} }

This works fine as shown, but I need to add the alpha channel to control
opacity. I've tried highlight_color = rgba(255,0,0,n) where 0<n<1, but it
throws an error. I've also tried =new Array(255,0,0,n) to no avail.
Please...what is the correct syntax?

Thanks,
--
Joey
3 Answers

JJ

5/22/2014 1:07:00 AM

0

On Wed, 21 May 2014 17:17:21 -0700, Joey@still_Learning.invalid wrote:
> I'm trying to specify a group of colors as RGBa, but I'm lost.
>
> Example code:
>
> highlight_color = new Array(255,0,0);
>
> for (var x = 0; x < width; x++) {
> for (var y = 0; y < height; y++) {
>
> gray_avg = ctx.getImageData(x, y, 1, 1).data[0];
>
> if (gray_avg >= threshold) { gray = highlight_color; }
> ....
> } }
>
> This works fine as shown, but I need to add the alpha channel to control
> opacity. I've tried highlight_color = rgba(255,0,0,n) where 0<n<1, but it
> throws an error. I've also tried =new Array(255,0,0,n) to no avail.
> Please...what is the correct syntax?
>
> Thanks,

The data property is an array of RGBA values. i.e.:
[RR, GG, BB, AA, RR, GG, BB, AA, ...]
^ pixel #0 ^ pixel #1

So, data[0] is the first pixel's red channel level, not the alpha channel.

e.g.:

var imgdata = ctx.getImageData(x, y, 1, 1).data;
var red = imgdata[0];
var green = imgdata[1];
var blue = imgdata[2];
var alpha = imgdata[3];

Joey@still_Learning.invalid

5/22/2014 2:56:00 AM

0

JJ wrote:

>On Wed, 21 May 2014 17:17:21 -0700, Joey@still_Learning.invalid wrote:
>> I'm trying to specify a group of colors as RGBa, but I'm lost.
>>
>> Example code:
>>
>> highlight_color = new Array(255,0,0);
>>
>> for (var x = 0; x < width; x++) {
>> for (var y = 0; y < height; y++) {
>>
>> gray_avg = ctx.getImageData(x, y, 1, 1).data[0];
>>
>> if (gray_avg >= threshold) { gray = highlight_color; }
>> ....
>> } }
>>
>> This works fine as shown, but I need to add the alpha channel to control
>> opacity. I've tried highlight_color = rgba(255,0,0,n) where 0<n<1, but it
>> throws an error. I've also tried =new Array(255,0,0,n) to no avail.
>> Please...what is the correct syntax?
>>
>> Thanks,
>
>The data property is an array of RGBA values. i.e.:
>[RR, GG, BB, AA, RR, GG, BB, AA, ...]
> ^ pixel #0 ^ pixel #1
>
>So, data[0] is the first pixel's red channel level, not the alpha channel.
>
>e.g.:
>
>var imgdata = ctx.getImageData(x, y, 1, 1).data;
>var red = imgdata[0];
>var green = imgdata[1];
>var blue = imgdata[2];
>var alpha = imgdata[3];

Thanks, JJ.

I failed to communicate my issue. I was simply looking for syntax, but no
matter. I found a much easier and better workaround using css3 opacity on
the canvas.
--
Joey

Joey@still_Learning.invalid

5/22/2014 2:57:00 AM

0

Joey@still_Learning.invalid wrote:

>I'm trying to specify a group of colors as RGBa, but I'm lost.
>
>Example code:
>
>highlight_color = new Array(255,0,0);
>
>for (var x = 0; x < width; x++) {
>for (var y = 0; y < height; y++) {
>
> gray_avg = ctx.getImageData(x, y, 1, 1).data[0];
>
>if (gray_avg >= threshold) { gray = highlight_color; }
>....
>} }
>
>This works fine as shown, but I need to add the alpha channel to control
>opacity. I've tried highlight_color = rgba(255,0,0,n) where 0<n<1, but it
>throws an error. I've also tried =new Array(255,0,0,n) to no avail.
>Please...what is the correct syntax?
>
Alternative method employed. Issue resolved
--
Joey