HP Forums
Color Values in Python? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Color Values in Python? (/thread-16708.html)



Color Values in Python? - toml_12953 - 04-21-2021 12:23 AM

In HPPL we can use the rgb() function to specify color. That doesn't work in Python. Is there a way to specify color in Python? I know I can do

65536*r + 256*g + b

but I'd like a better way.


RE: Color Values in Python? - critor - 04-21-2021 07:01 AM

The HPPPL rgb() function works perfectly in Python.

Here's a helper function for you :
Code:
import hpprime

def rgb(r,g,b):
  return hpprime.eval("rgb("+str(r)+","+str(g)+","+str(b)+")")



RE: Color Values in Python? - cyrille de brébisson - 04-21-2021 07:23 AM

Hello,

Honestly, the best way is to hard code the colors.
create yourself a const variable that has the colors that you want. This will be the fastest...

Else, define rgb as
def rgb(r,g,b):
return 65536*r + 256*g + b;

Please do NOT call hpprime primitives for such things, the performance hit will be enormous...
Understand that calling hpprime.eval as suggested means transforming integers to a string, concactenating strings (lots of memory moves and the like), then passing all that to the prime system (meaning switching from UTF8 to wchars), followed by parsing of the string, evaluation, and finally an analysis of the result to transform it back to python structures!

I know that prime allows easy use and switch between PPL, cas and python, but these should be used sparingly, only when absolutely nessecary, with large swats in an language or another, but small, repeted calles from one realm to the others are not a good idea.
They will be, at best slow, at worse highlighting bugs and causing problems...

Cyrille


RE: Color Values in Python? - toml_12953 - 04-21-2021 07:49 AM

(04-21-2021 07:23 AM)cyrille de brébisson Wrote:  Hello,

Honestly, the best way is to hard code the colors.
create yourself a const variable that has the colors that you want. This will be the fastest...

Else, define rgb as
def rgb(r,g,b):
return 65536*r + 256*g + b;

Please do NOT call hpprime primitives for such things, the performance hit will be enormous...
Understand that calling hpprime.eval as suggested means transforming integers to a string, concactenating strings (lots of memory moves and the like), then passing all that to the prime system (meaning switching from UTF8 to wchars), followed by parsing of the string, evaluation, and finally an analysis of the result to transform it back to python structures!

I know that prime allows easy use and switch between PPL, cas and python, but these should be used sparingly, only when absolutely nessecary, with large swats in an language or another, but small, repeted calles from one realm to the others are not a good idea.
They will be, at best slow, at worse highlighting bugs and causing problems...

Cyrille

I just found out the hard way! Try this in Python

hpprime.eval("rgb(0,255,0)")

My machine crashes with a conversion error.


RE: Color Values in Python? - critor - 04-21-2021 09:03 AM

Code:
hpprime.eval("rgb(0,255,0)")
No crash here.


RE: Color Values in Python? - toml_12953 - 04-21-2021 02:23 PM

(04-21-2021 09:03 AM)critor Wrote:  
Code:
hpprime.eval("rgb(0,255,0)")
No crash here.

I get "OverflowError: overflow converting long int to machine word" in this line (line 22 in the code). If I replace the eval with a number, I get no error.

pixon(0,x1,230-y1,eval("rgb(0,255,0)"))

Code:
#PYTHON EXPORT pyhat
from hpprime import *
from math import *
t0 = eval("ticks()") # Save the current clock count for timing program
# Clear screen
fillrect(0,0,0,320,240,0,0)
# Start program proper
p=160; q=120
xp=144; xr=1.5*3.1415927
yp=56; yr=1; zp=64
xf=xr/xp; yf=yp/yr; zf=xr/zp
for zi in range(-q,q+1):
  if zi>=-zp and zi<=zp:
    zt=zi*xp/zp; zz=zi
    xl=int(.5+sqrt(xp*xp-zt*zt))
    # Draw one cross-section of figure
    for xi in range(-xl,xl+1):
      xt=sqrt(xi*xi+zt*zt)*xf; xx=xi
      yy=(sin(xt)+.4*sin(3*xt))*yf
      x1=xx+zz+p
      y1=yy-zz+q
      pixon(0,x1,230-y1,eval("rgb(0,255,0)"))
      if y1!=0:
        line(0,x1,230-y1+1,x1,230,0) # Erase points below current point
t = eval("ticks()")-t0
# Wait for key and print elapsed time
eval("wait()")
t = t/1000
print(t," seconds")
#end



RE: Color Values in Python? - cyrille de brébisson - 04-22-2021 08:41 AM

Hello,

I found a big bug: eval("rgb(....)");
will NOT work well (at all)...
Basically rgb returns an integer (64 bit), not a real.
The conversion from int to python integer was messed up..
Python exects (for some reason) the data to be in little endian mode, not big endian...

Will be fixed in next version.
Cyrille


RE: Color Values in Python? - toml_12953 - 04-22-2021 01:26 PM

(04-22-2021 08:41 AM)cyrille de brébisson Wrote:  Hello,

I found a big bug: eval("rgb(....)");
will NOT work well (at all)...
Basically rgb returns an integer (64 bit), not a real.
The conversion from int to python integer was messed up..
Python exects (for some reason) the data to be in little endian mode, not big endian...

Will be fixed in next version.
Cyrille

Thanks for the quick response! I think I'll just write a Python rgb() function and use that, however.