Archive for the 'NSColorWell' Category

2018/01/04 Coloursで色バリエーション展開を計算して表示 v2

オープンソースの「Colours」(By Ben Gordon)をフレームワーク化したcolorsKitを呼び出して、指定色のカラーバリエーションを4通り計算して表示するAppleScriptです。


–> Download Framework Binary



Coloursが用意しているColorSchemeAnalagous, ColorSchemeMonochromatic, ColorSchemeTriad, ColorSchemeComplementaryの4通りのカラーバリエーション計算を行なってNSColorWellで表示します。

AppleScript名:Colorsで色バリエーション展開を計算して表示 v2
– Created 2017-12-20 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version “2.5″
use scripting additions
use framework “Foundation”
use framework “AppKit”
use framework “colorsKit” –

property NSView : a reference to current application’s NSView
property NSColor : a reference to current application’s NSColor
property NSString : a reference to current application’s NSString
property NSScreen : a reference to current application’s NSScreen
property NSButton : a reference to current application’s NSButton
property NSWindow : a reference to current application’s NSWindow
property NSColorWell : a reference to current application’s NSColorWell
property NSWindowController : a reference to current application’s NSWindowController

property windisp : false

set aWidth to 500
set aHeight to 250

set {rVal, gVal, bVal} to choose color
set aNSCol to makeNSColorFromRGBAval(rVal, gVal, bVal, 65536, 65536) of me

dispCustomView(aWidth, aHeight, “Color Variation Result”, “OK”, 180, aNSCol) of me

on dispCustomView(aWidth as integer, aHeight as integer, aTitle as text, aButtonMSG as text, timeOutSecs as number, aNSCol)
–Check If this script runs in foreground
if not (current application’s NSThread’s isMainThread()) as boolean then
    error “This script must be run from the main thread (Command-Control-R in Script Editor).”
  end if
set (my windisp) to true
set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(aWidth / 4, 0, aWidth / 2, 40)))
bButton’s setTitle:aButtonMSG
bButton’s setButtonType:(current application’s NSMomentaryLightButton)
bButton’s setBezelStyle:(current application’s NSRoundedBezelStyle)
bButton’s setKeyEquivalent:(return)
bButton’s setTarget:me
bButton’s setAction:(“clicked:”)
set aNSV to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth))
aNSV’s addSubview:bButton
aNSV’s setNeedsDisplay:true
repeat with ii from 0 to 3 by 1
    set colorArray1 to (aNSCol’s colorSchemeOfType:ii) as list
set the beginning of colorArray1 to aNSCol
set tmpLen to (length of colorArray1)
set aStep to 0
repeat with i in colorArray1
      set aColorWell to (NSColorWell’s alloc()’s initWithFrame:(current application’s NSMakeRect((100 * aStep), (((3 - ii) * 50) + 40), 100, 40)))
aColorWell’s setColor:i)
aColorWell’s setBordered:true)
aNSV’s addSubview:aColorWell)
set aStep to aStep + 1
    end repeat
  end repeat
set aWin to makeWinWithView(aNSV, aWidth, aHeight, aTitle, 1.0)
set wController to NSWindowController’s alloc()
wController’s initWithWindow:aWin
wController’s showWindow:me
aWin’s makeKeyAndOrderFront:me
set aCount to timeOutSecs * 10 –timeout seconds * 10
repeat aCount times
    if (my windisp) = false then
      exit repeat
    end if
delay 0.1
  end repeat
my closeWin:aWin
end dispCustomView

–Button Clicked Event Handler
on clicked:aSender
  set (my windisp) to false
end clicked:

–make Window for Input
on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV)
  set aScreen to NSScreen’s mainScreen()
set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
set aBacking to current application’s NSTitledWindowMask
set aDefer to current application’s NSBackingStoreBuffered
– Window
set aWin to NSWindow’s alloc()
aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
aWin’s setTitle:aTitle
aWin’s setDelegate:me
aWin’s setDisplaysWhenScreenProfileChanges:true
aWin’s setHasShadow:true
aWin’s setIgnoresMouseEvents:false
aWin’s setLevel:(current application’s NSNormalWindowLevel)
aWin’s setOpaque:false
aWin’s setAlphaValue:alphaV –append
aWin’s setReleasedWhenClosed:true
aWin’s |center|()
aWin’s makeKeyAndOrderFront:(me)
– Set Custom View
aWin’s setContentView:aView
return aWin
end makeWinWithView

–close win
on closeWin:aWindow
  repeat with n from 10 to 1 by -1
    (aWindow’s setAlphaValue:n / 10)
delay 0.02
  end repeat
aWindow’s |close|()
end closeWin:

on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
set aGreenCocoa to (greenValue / aMaxVal) as real
set aBlueCocoa to (blueValue / aMaxVal) as real
set aAlphaCocoa to (alphaValue / aMaxVal) as real
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
return aColor
end makeNSColorFromRGBAval

★Click Here to Open This Script