// adjusts the main color swatch

[Iconfig, 3.5 ] // specifies which version of ZB 
//[Iconfig, 3.1 ] // specifies which version of ZB 
[IShowActions, 0] // turn off UI visual clicks

////////////////////////////////////
// global vars
[VarDef, debug, 0] // turn on/off notes I use to check my math
[VarDef, hsv(3), 0]
[VarDef, rgb(3), 0]
[VarDef, init, 0]

////////////////////////////////////
// procs
////////////////////////////////////
[RoutineDef, rgbtohsv,
	[VarSet, r, (#rgb(0))/255 ]
	[VarSet, g, (#rgb(1))/255 ]
	[VarSet, b, (#rgb(2))/255 ]
	[VarSet, h, 0 ]
	[VarSet, s, 0 ]
	[VarSet, v, 0 ]
	[VarSet, x, 0 ]
	[VarSet, tr, 0 ]
	[VarSet, tg, 0 ]
	[VarSet, tb, 0 ]

	[VarSet, v, ( MAX((MAX(#r,#g)),#b) ) ]
	[VarSet, x, ( MIN((MIN(#r,#g)),#b) ) ]
	[If, (#v == #x),
		[VarSet, h, ( #v*6.0 ) ]
		[VarSet, s, 0.0]
	,
		[VarSet, s, ( (#v - #x)/#v ) ]
		[VarSet, h, 0.0]
		[VarSet, tr, ( (#v-#r)/(#v-#x) ) ]
		[VarSet, tg, ( (#v-#g)/(#v-#x) ) ]
		[VarSet, tb, ( (#v-#b)/(#v-#x) ) ]

		[If, (#r==#x),
			[VarSet, h, ( 3.0+(#tg-#tb) ) ]
		,
			[ If, (#g==#x),
				[VarSet, h, ( 5.0+(#tb-#tr) ) ]
			,
				[VarSet, h, ( 1.0+(#tr-#tg) ) ]
			]
		]
	]
	[VarSet, hsv(0), (#h*60) ] 
	[VarSet, hsv(1), #s ] 
	[VarSet, hsv(2), #v ] 
	[If, debug, [Note,[StrMerge, "RGB to HSV: ", #hsv(0)," ",#s," ",#v],,2] ]


]

[RoutineDef, hsvtorgb,
	[VarSet, h, ( (#hsv(0)) / 60.0 ) ]
	[VarSet, s, (#hsv(1))]
	[VarSet, v, (#hsv(2))]
	[VarSet, r, 0]
	[VarSet, g, 0]
	[VarSet, b, 0]
	[VarSet, f, 0]
	[VarSet, m, 0]
	[VarSet, n, 0]
	[VarSet, i, 0]

	[VarSet, i, ( INT(#h) ) ]
	[VarSet, f, ( #h-#i ) ]
	[If, (#i==0) || (#i==2) || (#i==4) || (#i == 6),
		[VarSet, f, ( 1.0-#f ) ]
	]
	[VarSet, m, ( #v * (1.0-#s) ) ]
	[VarSet, n, ( #v * (1.0 - (#s*#f)) ) ]
	[If, #i==0,
		[VarSet, r, #v]
		[VarSet, g, #n]
		[VarSet, b, #m]
	]
	[If, #i==1,
		[VarSet, r, #n]
		[VarSet, g, #v]
		[VarSet, b, #m]
	]
	[If, #i==2,
		[VarSet, r, #m]
		[VarSet, g, #v]
		[VarSet, b, #n]
	]
	[If, #i==3,
		[VarSet, r, #m]
		[VarSet, g, #n]
		[VarSet, b, #v]
	]
	[If, #i==4,
		[VarSet, r, #n]
		[VarSet, g, #m]
		[VarSet, b, #v]
	]
	[If, #i==5,
		[VarSet, r, #v]
		[VarSet, g, #n]
		[VarSet, b, #m]
	]
	[If, #i==6,
		[VarSet, r, #v]
		[VarSet, g, #v]
		[VarSet, b, #v]
	]

	[If, FRAC(#r*255.0)>=0.5, 
		[VarSet, #rgb(0), ((INT(#r*255))+1) ]
	,
		[VarSet,#rgb(0),(INT(#r*255))]
	]
	
	[If, FRAC(#g*255.0)>=0.5, 
		[VarSet, #rgb(1), ((INT(#g*255))+1) ]
	,
		[VarSet,#rgb(1),(INT(#g*255))]
	]

	[If, FRAC(#b*255.0)>=0.5, 
		[VarSet, #rgb(2), ((INT(#b*255))+1) ]
	,
		[VarSet,#rgb(2),(INT(#b*255))]
	]

	[If, debug, [Note,[StrMerge, "HSV to RGB: ", #r," ",#g," ",#b],,2] ]


]

// Grab the current activce RGB values and update the sliders
[RoutineDef, updateRgbHsv,
	[VarSet, init, 1] // the code in the sliders was getting called recursively, so I set a flag to prevent it.

	// grab the active color from the Color menu
	[VarSet, rgb(0), [IGet,Color:R] ]
	[VarSet, rgb(1), [IGet,Color:G] ]
	[VarSet, rgb(2), [IGet,Color:B] ]
	[If, debug, [Note,[StrMerge,#rgb(0)," ",#rgb(1)," ",#rgb(2)],,2] ]

	// set the RGB slider values
	[ISet, "Color:RGB HSV Sliders:Red", (#rgb(0)) ]
	[ISet, "Color:RGB HSV Sliders:Grn", (#rgb(1)) ]
	[ISet, "Color:RGB HSV Sliders:Blu", (#rgb(2)) ]

	// convert to HSV
	[RoutineCall,rgbtohsv]

	// set the HSV values
	[ISet, "Color:RGB HSV Sliders:Hue", (#hsv(0))]
	[ISet, "Color:RGB HSV Sliders:Sat", (#hsv(1))]
	[ISet, "Color:RGB HSV Sliders:Val", (#hsv(2))]

	[VarSet, init, 0] // slider update on
]

// Proc that runs after the user releases an HSV slider
[RoutineDef, hsvslide,
	[VarSet, init, 1] // slider update off

	// grab the HSV slider values
	[VarSet, hsv(0), [IGet, "Color:RGB HSV Sliders:Hue"] ]
	[VarSet, hsv(1), [IGet, "Color:RGB HSV Sliders:Sat"] ]
	[VarSet, hsv(2), [IGet, "Color:RGB HSV Sliders:Val"] ]

	// convert to RGB
	[RoutineCall,hsvtorgb]
	// set the RGB slider values
	[ISet, "Color:RGB HSV Sliders:Red", (#rgb(0))]
	[ISet, "Color:RGB HSV Sliders:Grn", (#rgb(1))]
	[ISet, "Color:RGB HSV Sliders:Blu", (#rgb(2))]

	// set the active color
	[IColorSet, #rgb(0), #rgb(1), #rgb(2)]
	[VarSet, init, 0] // slider update on
]

// Proc that runs after the user releases an RGB slider
[RoutineDef, rgbslide,
	[VarSet, init, 1] // slider update off

	// grab the RGB slider values
	[VarSet, rgb(0), [IGet, "Color:RGB HSV Sliders:Red"] ]
	[VarSet, rgb(1), [IGet, "Color:RGB HSV Sliders:Grn"] ]
	[VarSet, rgb(2), [IGet, "Color:RGB HSV Sliders:Blu"] ]

	// set the active color
	[IColorSet, #rgb(0), #rgb(1), #rgb(2)]

	// convert to HSV
	[RoutineCall,rgbtohsv]
	// set the HSV slider values
	[ISet, "Color:RGB HSV Sliders:Hue", (#hsv(0))]
	[ISet, "Color:RGB HSV Sliders:Sat", (#hsv(1))]
	[ISet, "Color:RGB HSV Sliders:Val", (#hsv(2))]
	[VarSet, init, 0] // slider update on
]


////////////////////////////////////
// UI
////////////////////////////////////

// It's easier for me to look up the UI code here rather than jumping back to the web page.  I'll take it out later

//[IButton,  Button name, Popup info Text, Commands group to execute when button is pressed, Initially Disabled? (0:Enabled(ByDefault) NonZero:Disabled), Button width in pixels (0:AutoWidth NonZero:Specified width), Optional hotkey, Optional button icon (.psd .bmp + .pct for Mac Systems), Button height in pixels (0:AutoHeight NonZero:Specified height)]

//[ISlider, Slider name, CurValue, Resolution, MinValue, MaxValue, Popup info Text, Commands group to execute when value is changed, Initially Disabled? (0:Enabled(ByDefault) NonZero:Disabled), Slider width in pixels (0:AutoWidth NonZero:Specified width)] 

//[ISwitch, Switch name , Initial state (1:pressed, 0:unpressed), Popup info Text, Commands group to execute when button is pressed , Commands group to execute when button is unpressed, Initially Disabled? (0:Enabled(ByDefault) NonZero:Disabled), Switch width in pixels (0:AutoWidth NonZero:Specified width)] 

//[SectionBegin, Section Title, Initial state (1:Expanded, 0:Collapsed ), Popup Info Text, Commands group to execute when expanding to reveal content, Commands group to execute when collapsing to hide content, Initially Disabled? (0:Enabled(ByDefault) NonZero:Disabled)]

// Sub Palette in the Color menu
[ISubPalette, "Color:RGB HSV Sliders",0]

[PenMoveDown] // not sure if I need this.  The first button was not showing up, so I forced a pen move.

// Hit the button to grab the current RGB values and set the sliders accordingly
[IButton, "Color:RGB HSV Sliders:Update RGB", "Gets the active RGB color.",
	[RoutineCall,updateRgbHsv]
	,0,150,,,0
] 

// RGB sliders
[ISlider, "Color:RGB HSV Sliders:Red", 0, 1, 0, 255, "Adjust RED", 
	[If, (#init==0),
		[RoutineCall, rgbslide]
	]
,,200
]

[ISlider, "Color:RGB HSV Sliders:Grn", 0, 1, 0, 255, "Adjust GREEN", 
	[If, (#init==0),
		[RoutineCall, rgbslide]
	]
,,200
]

[ISlider, "Color:RGB HSV Sliders:Blu", 0, 1, 0, 255, "Adjust BLUE",
	[If, (#init==0),
		[RoutineCall, rgbslide]
	]
,,200
]

// HSV sliders
[ISlider, "Color:RGB HSV Sliders:Hue", 0, 0, 0, 360, "Adjust the Hue",
	[If, (#init==0),
		[RoutineCall, hsvslide]
	]
,,200
]

[ISlider, "Color:RGB HSV Sliders:Sat", 0, 0, 0, 1, "Adjust the Saturation",
	[If, (#init==0),
		[RoutineCall, hsvslide]
	]
,,200
]

[ISlider, "Color:RGB HSV Sliders:Val", 0, 0, 0, 1, "Adjust the Value",
	[If, (#init==0),
		[RoutineCall, hsvslide]
	]
,,200
]

// Update the sliders when the proc code is run for the first time.
// Probably will remove this later.
[RoutineCall,updateRgbHsv]
