Color Values in Python?
04-21-2021, 12:23 AM (This post was last modified: 04-21-2021 01:31 AM by toml_12953.)
Post: #1
 toml_12953 Senior Member Posts: 1,947 Joined: Dec 2013
Color Values in Python?
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.

Tom L
Cui bono?
04-21-2021, 07:01 AM (This post was last modified: 04-21-2021 07:02 AM by critor.)
Post: #2
 critor Member Posts: 54 Joined: Jul 2014
RE: Color Values in Python?
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)+")")
04-21-2021, 07:23 AM
Post: #3
 cyrille de brébisson Senior Member Posts: 1,047 Joined: Dec 2013
RE: Color Values in Python?
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

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
04-21-2021, 07:49 AM (This post was last modified: 04-21-2021 07:52 AM by toml_12953.)
Post: #4
 toml_12953 Senior Member Posts: 1,947 Joined: Dec 2013
RE: Color Values in Python?
(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.

Tom L
Cui bono?
04-21-2021, 09:03 AM
Post: #5
 critor Member Posts: 54 Joined: Jul 2014
RE: Color Values in Python?
Code:
hpprime.eval("rgb(0,255,0)")
No crash here.
04-21-2021, 02:23 PM
Post: #6
 toml_12953 Senior Member Posts: 1,947 Joined: Dec 2013
RE: Color Values in Python?
(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

Tom L
Cui bono?
04-22-2021, 08:41 AM
Post: #7
 cyrille de brébisson Senior Member Posts: 1,047 Joined: Dec 2013
RE: Color Values in Python?
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

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
04-22-2021, 01:26 PM (This post was last modified: 04-22-2021 01:43 PM by toml_12953.)
Post: #8
 toml_12953 Senior Member Posts: 1,947 Joined: Dec 2013
RE: Color Values in Python?
(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.

Tom L
Cui bono?
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)