It is currently Wed Jul 24, 2024 2:21 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: testing FG and/or BG colors
PostPosted: Fri Feb 23, 2018 3:48 pm  (#1) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
GIMP Version: 2.8.22
Operating System: Windows
GIMP Experience: Basic Level

List any relevant plug-ins or scripts:
any py script

List any ERROR messages you received:
TypeError: can't compare gimpcolor.RGB to tuple



Likely a silly question for an expert, but I don't have an answer:
- how to test the "values" of -for instance- the FG colour in just one line?
So far I always unpacked the r,g,b values and tested each one separately, but I would like to have a simpler way, such as:
if (gimp.get_foreground() == ((0,0,0,255))): ....
which is not accepted ("gimpcolor.RGB can't be compared with a tuple").
In my simple mind I do not understand why it is possible to set a foreground colour to ((0,0,0,255)), but it cannot be retrieved/compared in the same way.
Thanks for your help.

_________________
"Where am I ?"


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: testing FG and/or BG colors
PostPosted: Fri Feb 23, 2018 4:21 pm  (#2) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4758
Yes, the foreground color you get is of type gimpcolor.RGB so you have to compare it with another object of the same type, so instead of using a plain tuple, you just create an instance of gimpcolor.RGB with the proper values. Note that gimpcolor is a separate module so you have to import it:

import gimpcolor
if (gimp.get_foreground() == gimpcolor.RGB(0,0,0,255):


Two remarks:

1) like in many other places, the gimpcolor.RGB contructor takes either floating point in the [0.-1.] range or integer in the [0-255] range (but with Gimp 2.9/2.10 it will make more sense to use the floating point values).
So the code above could be written, with the same results:

import gimpcolor
if (gimp.get_foreground() == gimpcolor.RGB(0.,0.,0.,1.):


2) you can avoid importing the gimpcolor module. In an object there is always a __class__ attribute to refer to the object's class, and this class can be used to call the constructor, so you can avoid the explicit import and use

fg=gimp.get_foreground()
if fg==fg.__class__(0,0,0,255):

_________________
Image


Top
 Post subject: Re: testing FG and/or BG colors
PostPosted: Sat Feb 24, 2018 12:14 am  (#3) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
Thanks a lot, ofnuts.
The first solution proposed is the one I like better, that was exactly the info I needed.

_________________
"Where am I ?"


Top
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) questions about testing with DIEGO_Render_FillingPlus

13

No new posts Testing Gmic-Qt Standalone 3.1

4

No new posts Attachment(s) Amazing what can be found under testing

30

No new posts Attachment(s) New layer modes in LayerFX-2.10 for testing.

9

No new posts Attachment(s) My Experiments On GMIC Testing filters: Simple Graphics (now 14)

1046



* Login  



Powered by phpBB3 © phpBB Group