Omnimaga

Calculator Community => HP Calculators => Topic started by: DJ Omnimaga on August 27, 2013, 02:06:02 am

Title: Splitting colors/getting the value of a part of a color
Post by: DJ Omnimaga on August 27, 2013, 02:06:02 am
Does anyone know how to extract the value of a color? For example, if I use GETPIX_P(X,Y), how would I access the value of the blue color in particular, so that I can modify it?

I am curious about changing each value separately, since it might make it easier to do some graphical effects such as fade-in/outs or whatever. I would prefer not having to use divisions, since they seem much slower (but again, this is the emulator, so maybe other means are as slow on the real calc anyway).

This is what I have right now, by the way:

Code: [Select]
EXPORT fadein()
BEGIN
FOR C FROM 0 TO 3 DO
FOR X FROM 0 TO 319 DO
FOR Y FROM 0 TO 239 DO
INT(GETPIX_P(X,Y)/2)▶P;
PIXON_P(X,Y,P);
END;
END;
END;
END;
Title: Re: Splitting colors/getting the value of a part of a color
Post by: Matrefeytontias on August 27, 2013, 07:33:37 am
IIRC, the HP Prime's screen uses 16-bits colors (R5G6B5 format), so you can't access  colors separately unless you use bitwise operators (and, or, not) and bit shifting. I don't think HP basic permits you to do that.
Title: Re: Splitting colors/getting the value of a part of a color
Post by: Jim Bauwens on August 27, 2013, 07:37:00 am
If you divide by 2^n (and remove the remainder, with something like int() ) it's the same like shifting with n. And then you can use the modulus operator to get the bits you want. (So basically you simulate bitwise operators).
Title: Re: Splitting colors/getting the value of a part of a color
Post by: DJ Omnimaga on August 27, 2013, 02:57:17 pm
IIRC, the HP Prime's screen uses 16-bits colors (R5G6B5 format), so you can't access  colors separately unless you use bitwise operators (and, or, not) and bit shifting. I don't think HP basic permits you to do that.
Actually it's A1R5G5B5, due to transparency being supported (per individual pixel basis).
If you divide by 2^n (and remove the remainder, with something like int() ) it's the same like shifting with n. And then you can use the modulus operator to get the bits you want. (So basically you simulate bitwise operators).
What would n be for? Would it have to be 5 (the amount of bits per color)? Also what about the alpha bit?
Title: Re: Splitting colors/getting the value of a part of a color
Post by: lkj on August 27, 2013, 03:16:54 pm
For the blue part you would do: color MOD 2^5
For the green part IP(color / 2^5) MOD 2^5
And for the red part IP(color / 2^10) MOD 2^5
For the alpha: IP(color / 2^15)

Edit: IP is the integer part function on the prime
Title: Re: Splitting colors/getting the value of a part of a color
Post by: Matrefeytontias on August 27, 2013, 03:17:17 pm
Then you'd use something like that :
Code: [Select]
.alpha = int(color / 32768)
red = int(color / 1024) % 32
green = int(color / 32) % 32
blue = color % 32
l
Alpha is now the 15th bit of the color, red is the 10th to 14th bits, green is the 5th to 9th bit and blue the 0th to 4th bit. You can also build a color this way :
Code: [Select]
color = (alpha % 2) * 32768 + (red % 32) * 1024 + (green % 32) * 32 + (blue % 32)Assuming % is the modulo operator.

EDIT : ninja'd
Title: Re: Splitting colors/getting the value of a part of a color
Post by: DJ Omnimaga on August 28, 2013, 03:04:43 am
Ok thanks, I'll see how to do this on an HP Prime when I have time (hoping there's a mod command)
Title: Re: Splitting colors/getting the value of a part of a color
Post by: lkj on August 28, 2013, 09:31:31 am
I tested my code in the prime emu, it should work ;)
Title: Re: Splitting colors/getting the value of a part of a color
Post by: Gilles59 on August 28, 2013, 05:35:07 pm
Hi all !

I don't understand how the alpha channel works (it use 1 bit or 3 bits ? one bit per channel color ?) ...

However, this  works for RGB :

Code: [Select]
EXPORT Red(c) BEGIN BITAND(BITSR(c,16),#7Fh); END;
EXPORT Green(c) BEGIN BITAND(BITSR(c,8),#7Fh); END;
EXPORT Blue(c) BEGIN BITAND(c,#7Fh); END;

I think it will be faster than divisions and modulo. Note that there is no need of RETURN keyword as the Prime always returns its last calculation

Quote
the HP Prime's screen uses 16-bits colors (R5G6B5 format), so you can't access  colors separately unless you use bitwise operators (and, or, not) and bit shifting. I don't think HP basic permits you to do that.

Seems to be  24 bits colors scheme with alpha transparency. It seems that you can define for each R G B color if there is transparency or not
So you have 128 x 128 x 128 colors,and 1 bit of tranparency for each R V B color ( it's just a guess ...)


This one is a lot faster :

Code: [Select]
EXPORT fadein()
BEGIN
 FOR C FROM 0 TO 3 DO
  FOR X FROM 0 TO 319 DO
   FOR Y FROM 0 TO 239 DO
    PIXON_P(X,Y,BITSR(GETPIX_P(X,Y)));  //avoid division and  variable ...
   END;
 END;
 END;
END;

By the way I wonder what happen when you use 'transparency' and copy a grob in another grob with the BLIT_P command ?
Title: Re: Splitting colors/getting the value of a part of a color
Post by: DJ Omnimaga on August 29, 2013, 01:54:43 am
Actually the HP Prime colors are A1R5G5B5, meaning 1 bit for the alpha channel and 5 bits for each color. However, when we use the RGB command, it seems to be multiplied so that every value can be up to 255.
Title: Re: Splitting colors/getting the value of a part of a color
Post by: DJ Omnimaga on August 29, 2013, 04:26:40 pm
I tried your last code by the way and it works :D. Question, though: What is the BITSC command and what does it do? I couldn't find anything about it in the Wiki nor the calculator help. I did a search for BITSC and nothing could be found.

Also, could anyone try this on a real calc if they got a sample, just to make sure that it's actually fast enough to be useable in some cutscenes? A video would be nice.
Title: Re: Splitting colors/getting the value of a part of a color
Post by: Gilles59 on August 29, 2013, 04:57:24 pm
I tried your last code by the way and it works :D. Question, though: What is the BITSC command and what does it do? I couldn't find anything about it in the Wiki nor the calculator help. I did a search for BITSC and nothing could be found.


BITxx are bits manipulation commands . BITSR is bitwise shift right and it's the same than divide by 2 with a binary number.

1010101010b  -> 101010101b

To get the syntax on the Prime

push  the 'tool box'  key ( the same with MEM in blue)
tap 'Catlg' soft key to acces to all the Prime commands
search the command (scroll or write the first letters)
then push HELP
Title: Re: Re: Splitting colors/getting the value of a part of a color
Post by: DJ Omnimaga on August 29, 2013, 09:29:14 pm
Ooh right, I see now! Also I got another idea for faster fade out: Rather than doing it pixel by pixel on the entire 320x240 screen, make a copy of your sprite sheet, then fade that smaller image out instead. In the process, keep redrawing your tilemap with that sprite sheet instead of the default one. :P

EDIT: THis is what it gives on the emulator with the modified code above and some extra random code. I am curious about how fast it is on the calc lol:

(http://img.ourl.ca//pixelate.gif)

Code: [Select]
EXPORT pixelate()
BEGIN
 2▶A;
 DIMGROB_P(G1,320,240);
 DIMGROB_P(G2,320,240);
 BLIT(G2,G0);
 FOR C FROM 1 TO 4 DO
  FOR X FROM 0 TO 319 DO
   FOR Y FROM 0 TO 239 DO
    PIXON_P(G1,X,Y,BITSR(GETPIX_P(X,Y)));
   END;
 END;
 BLIT_P(G1,0,0,320/A,240/A,G1,0,0,320,240);
 BLIT_P(G0,0,0,320,240,G1,0,0,320/A,240/A);
 A*2▶A
 END;
 FOR C FROM 80 DOWNTO .50 STEP .25 DO
 BLIT_P(G1,0,0,4*C,3*C,G2,0,0,320,240);
 BLIT_P(G0,0,0,320,240,G1,0,0,4*C,3*C);
 END;
END;
Title: Re: Splitting colors/getting the value of a part of a color
Post by: Gilles59 on August 30, 2013, 12:46:59 pm
The begin is slow but the end is funny

http://www.dailymotion.com/video/x13z0bv_prime_tech (http://www.dailymotion.com/video/x13z0bv_prime_tech)

Sorry for poor quality video. It's better on the screen ;)
Title: Re: Splitting colors/getting the value of a part of a color
Post by: Adriweb on August 30, 2013, 12:53:42 pm
Ah, that's indeeed pretty cool :)
Title: Re: Re: Splitting colors/getting the value of a part of a color
Post by: DJ Omnimaga on August 30, 2013, 04:53:06 pm
Wow the 2nd part is pretty fast.