It is currently Fri Apr 26, 2024 9:04 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: sorting colors in gimp scheme scripts? (question)
PostPosted: Thu May 28, 2015 5:14 pm  (#1) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
How would i go about sorting colors?

I wrote a dumb script that just blindly randomly colorize indexed colors found here ->auto colorize script

What i want to do now or plan to do
is sort the indexed colors from darkest to lightest based on luminance (found an equation for luminance on the internet somewhere).
then randomly generate the same number of colors (used to colorize with) and then sort these colors based on luminance as well and then loop through the indexed colors darkest to brightest and colorize with my generated random colors.
That way my darkest luminance random color will be used to colorize the darkest indexed color and so on to the brightest one.

I'll probably be working vectors instead of list since i think it's easier to set and get values from a vector.
So if i manually created a vector of say for example 3 colors but will have like 12 elements because 1 for calculation of luminance, R, G, B so 4 for each color..
so how do i sort based on the first element(luminance) of each color ....
so if i had something like (L1,R1,G1,B1,L2,R2,G2,B2,L3,R3,G3,B3)...
how do i sort the vector so that it swaps all the RGB along with L value around values based on the L value)

I guess i could manually hack it and manually do it myself but i was hoping there's something already prebuilt for this maybe?

Thanks in advance.

_________________
TinT


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: sorting colors in gimp scheme scripts? (question)
PostPosted: Thu May 28, 2015 5:35 pm  (#2) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
also question 2.
I tried using this code as testing to see what happens when you colorize with different values of reds
on an image that just has one color.
(gimp-image-select-rectangle image CHANNEL-OP-REPLACE 0 0 50 50)
        (script-fu-colorize image layer (list 255 0 0) 100)
        (gimp-image-select-rectangle image CHANNEL-OP-REPLACE 50 0 100 50)
        (script-fu-colorize image layer (list 128 0 0) 100)
        (gimp-image-select-rectangle image CHANNEL-OP-REPLACE 100 0 150 50)
        (script-fu-colorize image layer (list 64 0 0) 100)

i expected these colorize calls to produce different shades since i gave it a different value of reds each time...
but they all came out the same...
if this is supposed to happen then how do i determine the luminance of a dark red since that luminance is obviously lower than a luminance of a higher red value.
How do determine the actual color of what is being used for colorize so that i can precalculate its luminance based on that? i hope i am explaining it right because these calls coming out the same really confused me.

_________________
TinT


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Thu May 28, 2015 5:50 pm  (#3) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
also is there a place on the internet that has all the functions in gimp scheme script located...
like not the procedures but the built-in functions.. like (rand) for example..
so i can browse and see if there's anything function on getting an an HSB color
like (getHSBColor hue, saturation, luminance) for example

_________________
TinT


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Thu May 28, 2015 6:52 pm  (#4) 
Offline
Global Moderator
User avatar

Joined: Nov 16, 2011
Posts: 5128
Location: Metro Vancouver, BC
Try, Gimp Developer Resources

_________________
Image
Gimp 2.8.18, Linux, median user
Gimp Chat Tutorials Index
Spirit Bear (Kermode)


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Fri May 29, 2015 2:06 am  (#5) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
trandoductin wrote:
How would i go about sorting colors?

I wrote a dumb script that just blindly randomly colorize indexed colors found here ->auto colorize script

What i want to do now or plan to do
is sort the indexed colors from darkest to lightest based on luminance (found an equation for luminance on the internet somewhere).
then randomly generate the same number of colors (used to colorize with) and then sort these colors based on luminance as well and then loop through the indexed colors darkest to brightest and colorize with my generated random colors.
That way my darkest luminance random color will be used to colorize the darkest indexed color and so on to the brightest one.

I'll probably be working vectors instead of list since i think it's easier to set and get values from a vector.
So if i manually created a vector of say for example 3 colors but will have like 12 elements because 1 for calculation of luminance, R, G, B so 4 for each color..
so how do i sort based on the first element(luminance) of each color ....
so if i had something like (L1,R1,G1,B1,L2,R2,G2,B2,L3,R3,G3,B3)...
how do i sort the vector so that it swaps all the RGB along with L value around values based on the L value)

I guess i could manually hack it and manually do it myself but i was hoping there's something already prebuilt for this maybe?

Thanks in advance.


I don't know Scheme but all evolved languages have some "sort" function that takes two arguments: a list/array to sort and a "compare" function. This "compare" function takes two arguments (that will be elements of the list) and returns -1/0/+1 depending on which element it consider the biggest. You write the "compare" function (that will very often be a mere simple comparison of two attributes of the lists elements) to make the sort function sort on your chosen criteria. The "sort" function uses it to compare elements in the list. In your case the list should be a lists of RGB lists "((R,G,B),(R,G,B),(R,G,B)...) or of LRGB is you want to pre-compute the luminosity (but this can be done in the "compare" function).

_________________
Image


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Fri May 29, 2015 2:17 am  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
trandoductin wrote:
also is there a place on the internet that has all the functions in gimp scheme script located...
like not the procedures but the built-in functions.. like (rand) for example..
so i can browse and see if there's anything function on getting an an HSB color
like (getHSBColor hue, saturation, luminance) for example


For the built-in functions of the TinyScheme language:

http://www.ve3syb.ca/wiki/doku.php?id=software:sf:start

in particular the "internally defined functions" link.

You won't find built-in function for color operations outside of the PDB. If they existed they would be documented. You'll find the C source code but you'll have to understand it and reimplement it in Scheme.

_________________
Image


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Fri May 29, 2015 7:34 am  (#7) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Here is the best sort function I've come up with.
(define (sort < lis)
    (define (dosort < lis n)
       (if (= n 1)
         (list (car lis))
         (let ((i (quotient n 2)))
           (domerge <
                    (dosort < lis i)
                    (dosort < (list-tail lis i) (- n i))))))
     (define (domerge < lis1 lis2)
       (cond
         ((null? lis1)
           lis2)
         ((null? lis2)
           lis1)
         ((< (car lis2) (car lis1))
           (cons (car lis2) (domerge < lis1 (cdr lis2) )))
         (else
           (cons (car lis1) (domerge < (cdr lis1) lis2)))))
     (if (null? lis)
       lis
       (dosort < lis (length lis))))


This sort might be used as shown in the following:

(let* ((cmap (let loop ((colors (vector->list (cadr (gimp-image-get-colormap image))))
                        (result '()))
               (if (null? colors)
                 result
                 (loop (cdddr colors)
                       (cons (list (car colors) (cadr colors) (caddr colors))
                             result)))))
       (sorteds (sort (lambda (a b) (< (+ (apply max a) (apply min a))
                                       (+ (apply max b) (apply min b))))
                      cmap)))
  ; DO STUFF WITH sorteds HERE
  )


This code is not optimal as the lightness calculation* would be performed multiple times as the list is sorted. Ofnuts's suggestion of appending the calculated lightness to each color in the rgb list would allow just a single calculation per color (whereas there would be roughly log2 N calculations per color for the above code, where N is the number of colors).

Might I suggest an alternative approach. Instead of matching the similarity of lightnesses of the colors in your two palettes, instead you match the distances between the colors (in the rgb color space). This is done automatically by GIMP when you paste into an Indexed image -- your script only needs to create a new Indexed image, set its colormap to your new palette, then paste in your original image. At a minimum, it would be interesting to see the difference in results of the two approaches.


* Actually, the calculation shown is for twice the lightness value, but since this is only used for comparison, the division by two of both operands is unneeded.

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Fri May 29, 2015 10:39 am  (#8) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
Thanks guys,

another question is.... any of you guys ran across better luminosity formula?

so far i tried these two...and the 2nd one seems the best right now.
(0.299*R + 0.587*G + 0.114*B)
sqrt( 0.299*R^2 + 0.587*G^2 + 0.114*B^2 )

but sometimes when i turn it to gray scale i can still it jumps from light back to a little darker than gradually light again..when i expected it to be gradually dark to light all the time.

I just wonder if there's a better or more correct luminosity equation so that after i randomize the colors or the same luminosity and turn it to gray scale it's still accurate.

see what i mean below I have a gradient from black to white... so it should be darkest to brightest.
but when i randomly colorize it (creating colors of what's supposed to be same luminosity)
I get this
Image
when i convert to gray scale mode i get this
Image
See? how it goes generally from dark to light but except the odd ones in the middle that are darker than the color on it's left?
I am looking for a luminosity function that when after i randomize the colors it'll give me an a proper gradient from dark to light without jumping back to a darker color in the middle.
and so that if i gray scale it it should give me something like this
Image

_________________
TinT


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Fri May 29, 2015 11:12 am  (#9) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
In Python the gimpcolor.RGB objects (those that are in a palette) have a luminance() method (and also a distance(some_other_color) one).

_________________
Image


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Fri May 29, 2015 11:24 am  (#10) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
is a formula available...i am just lazy don't want to relearn python and write another plugin.
:hehe

_________________
TinT


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Fri May 29, 2015 11:34 am  (#11) 
Offline
Script Coder
User avatar

Joined: Dec 27, 2014
Posts: 508
If you just want simple brightness, you don't need the weighting factors on the three channels.
Try:
sqrt( R^2 + G^2 + B^2 )


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Fri May 29, 2015 12:57 pm  (#12) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
I did a test with pure blue, pure red and pure green to guess at how GIMP converts to gray scale mode.
and it seems and i got 18 gray for blue, 54 gray value for red and 182 gray for green.
so i used these constants.
(B (/ 18.0 255))
(R (/ 54.0 255))
(G (/ 182.0 255))

and used the straight formula Luminance = red*R + green*G + blue*B
and so i successfully got it to work, so that when i gray scale it it's true to grayscale mode of original image.

_________________
TinT


Top
 Post subject: Re: sorting colors in gimp scheme scripts? (question)
PostPosted: Fri May 29, 2015 4:57 pm  (#13) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4739
trandoductin wrote:
is a formula available...i am just lazy don't want to relearn python and write another plugin.
:hehe


I believe they use the same formulas you'll find everywhere (see WIkipedia for the formulas used in the various colour models).

_________________
Image


Top
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Did something change in scheme in 2-10-20[Solved]

5

No new posts Attachment(s) Falling snow animated Scheme Script for Gimp

121

No new posts Attachment(s) Colors/Exposure

1

No new posts Attachment(s) Colors and Patchwork

2

No new posts Attachment(s) Can not see the colors and patterns SOLVED

4



* Login  



Powered by phpBB3 © phpBB Group