Author Topic: Splitting colors/getting the value of a part of a color  (Read 7871 times)

0 Members and 1 Guest are viewing this topic.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Splitting colors/getting the value of a part of a color
« 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;
« Last Edit: August 27, 2013, 02:17:23 am by DJ Omnimaga »

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: Splitting colors/getting the value of a part of a color
« Reply #1 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.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Splitting colors/getting the value of a part of a color
« Reply #2 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).

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Splitting colors/getting the value of a part of a color
« Reply #3 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?

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Splitting colors/getting the value of a part of a color
« Reply #4 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
« Last Edit: August 27, 2013, 03:18:59 pm by lkj »

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: Splitting colors/getting the value of a part of a color
« Reply #5 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
« Last Edit: August 27, 2013, 03:23:12 pm by Matrefeytontias »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Splitting colors/getting the value of a part of a color
« Reply #6 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)

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Splitting colors/getting the value of a part of a color
« Reply #7 on: August 28, 2013, 09:31:31 am »
I tested my code in the prime emu, it should work ;)

Offline Gilles59

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +1/-0
    • View Profile
Re: Splitting colors/getting the value of a part of a color
« Reply #8 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 ?
« Last Edit: August 28, 2013, 06:13:48 pm by Gilles59 »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Splitting colors/getting the value of a part of a color
« Reply #9 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.
« Last Edit: August 29, 2013, 01:57:03 am by DJ Omnimaga »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Splitting colors/getting the value of a part of a color
« Reply #10 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.

Offline Gilles59

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +1/-0
    • View Profile
Re: Splitting colors/getting the value of a part of a color
« Reply #11 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
« Last Edit: August 29, 2013, 05:05:22 pm by Gilles59 »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Re: Splitting colors/getting the value of a part of a color
« Reply #12 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:



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;
« Last Edit: August 29, 2013, 11:21:13 pm by DJ Omnimaga »

Offline Gilles59

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 8
  • Rating: +1/-0
    • View Profile
Re: Splitting colors/getting the value of a part of a color
« Reply #13 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

Sorry for poor quality video. It's better on the screen ;)
« Last Edit: August 30, 2013, 12:48:59 pm by Gilles59 »

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Splitting colors/getting the value of a part of a color
« Reply #14 on: August 30, 2013, 12:53:42 pm »
Ah, that's indeeed pretty cool :)
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation