Thursday, February 25, 2021

#1 2020-12-02 01:32:25 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

NSColor, AppleScript Colors...

Hi All, I think I need some help.

I'm working on a Script to load color palette in this example I have choose 'Apple'
I have extract the keys and running a repeat loop to get the values/components.
The item 2 of Apple color palette is color blue with color {7, 115, 65418} from choose color.

When I run the below script the value from the r,g,b is not correct.
I believe AppleScript use 0 - 65535 for 8-bits color.

Have I missing something ??

This is the result I get from Apple color palette
{{0, 0, 0}, {1101, 12999, 65535}, {43777, 31137, 16947}, {0, 64974, 65535}, {0, 64015, 0}, {65535, 16567, 65535}, {65535, 37773, 0}, {37957, 8389, 37531}, {65535, 9773, 0}, {65497, 64588, 0}, {65535, 65535, 65535}}

Applescript:

(**
* [Instancee Property]: allKeys
*    A new array containing the dictionary’s keys, or an empty array if the dictionary has no entries.
*)

set colorListName to current application's NSColorList's colorListNamed:"Apple"
set theColorList to (colorListName's allKeys()) as list
log theColorList

set colorList to {}
repeat with i in theColorList
   set colorName to contents of i
   -- Returns the color object associated with the specified key.
   set itsValue to ((colorListName's colorWithKey:colorName)'s {redComponent(), greenComponent(), blueComponent()})
   set {r, g, b} to itsValue
   set itsValue to {round (r * 65535), round (g * 65535), round (b * 65535)}
   copy itsValue to end of the colorList
end repeat
-- item 2 should be {7, 115, 65418}
return colorList

HERE IS THE CODE AFTER SHANE'S INPUT.

Applescript:

use framework "Foundation"
use framework "AppKit"
use scripting additions

set colorListName to current application's NSColorList's colorListNamed:"Apple"
set theColorList to (colorListName's allKeys()) as list
log theColorList

set colorListValues to {}
repeat with i in theColorList
   set colorName to contents of i
   -- Returns the color object associated with the specified key.
   set itsName to ((colorListName's colorWithKey:colorName)'s colorUsingColorSpace:(current application's NSColorSpace's genericRGBColorSpace()))
   set colorComp to {(itsName's redComponent()) * 65535 div 1, (itsName's greenComponent()) * 65535 div 1, (itsName's blueComponent()) * 65535 div 1}
   copy colorComp to end of the colorListValues
end repeat
return colorListValues

Regards.
Fredrik

Last edited by Fredrik71 (2020-12-03 02:41:36 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#2 2020-12-02 06:03:15 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6565

Re: NSColor, AppleScript Colors...

The value is correct -- just in a different color space.

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set colorList to current application's NSColorList's colorListNamed:"Apple"
set theBlue to colorList's colorWithKey:"Blue"
set comps1 to {(theBlue's redComponent()) * 65535 div 1, (theBlue's greenComponent()) * 65535 div 1, (theBlue's blueComponent()) * 65535 div 1}
set theBlue to theBlue's colorUsingColorSpace:(current application's NSColorSpace's genericRGBColorSpace())
set comps2 to {(theBlue's redComponent()) * 65535 div 1, (theBlue's greenComponent()) * 65535 div 1, (theBlue's blueComponent()) * 65535 div 1}
return {comps1, comps2}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-12-03 07:38:44 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Re: NSColor, AppleScript Colors...

Shane Stanley wrote:

The value is correct -- just in a different color space.


Thanks Shane as always you make a simple case to show it so clearly...

ColorSpace are interesting and also Color theory

Here is a script to show every ColorSpaceModels in localizedName

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set colorSpaceModels to current application's NSColorSpace's availableColorSpacesWithModel:(current application's NSColorSpaceModelUnknown)
log (colorSpaceModels's localizedName) as list


The purpose to study someone else art is not to add, its to make less more.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)