 
 ! column major order
 
-: gl-flip ( angle -- ) deg>rad
-{ [ dup 2 * cos ] [ dup 2 * sin ] 0 0
-  [ dup 2 * sin ] [ 2 * cos neg ] 0 0
-  0 0 1 0
-  0 0 0 1 } make* >c-double-array glMultMatrixd ;
+: gl-flip ( angle -- ) deg>rad dup dup dup
+  [ 2 * cos ,   2 * sin ,       0 ,   0 ,
+    2 * sin ,	2 * cos neg ,   0 ,   0 ,
+          0 ,             0 ,   1 ,   0 , 
+	  0 ,		  0 ,	0 ,   1 , ]
+  { } make >c-double-array glMultMatrixd ;
 
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 : circle ( -- )
-color> gl-set-hsba
-gluNewQuadric dup 0 0.5 20 10 gluDisk gluDeleteQuadric ;
+  color> gl-set-hsba
+  gluNewQuadric dup 0 0.5 20 10 gluDisk gluDeleteQuadric ;
 
 : triangle ( -- )
-color> gl-set-hsba
-GL_POLYGON glBegin
-   0    0.577 glVertex2d
-   0.5 -0.289 glVertex2d
-  -0.5 -0.289 glVertex2d
-glEnd ;
+  color> gl-set-hsba
+  GL_POLYGON glBegin
+    0    0.577 glVertex2d
+    0.5 -0.289 glVertex2d
+   -0.5 -0.289 glVertex2d
+  glEnd ;
 
 : square ( -- )
-color> gl-set-hsba
-GL_POLYGON glBegin
-  -0.5  0.5 glVertex2d
-   0.5  0.5 glVertex2d
-   0.5 -0.5 glVertex2d
-  -0.5 -0.5 glVertex2d
-glEnd ;
+  color> gl-set-hsba
+  GL_POLYGON glBegin
+    -0.5  0.5 glVertex2d
+     0.5  0.5 glVertex2d
+     0.5 -0.5 glVertex2d
+    -0.5 -0.5 glVertex2d
+  glEnd ;
 
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 : size ( scale -- ) dup 1 glScaled ;
 
-: s ( scale -- ) size ;
-
 : size* ( scale-x scale-y -- ) 1 glScaled ;
 
-: s* ( scale-x scale-y -- ) size* ;
-
 : rotate ( angle -- ) 0 0 1 glRotated ;
 
-: r ( angle -- ) rotate ;
-
 : x ( x -- ) 0 0 glTranslated ;
 
 : y ( y -- ) 0 swap 0 glTranslated ;
 
 : flip ( angle -- ) gl-flip ;
 
-: f ( angle -- ) flip ;
+! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+: s  size ;
+: s* size* ;
+: r  rotate ;
+: f  flip ;
 
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
 : do ( quot -- )
-push-modelview-matrix
-push-color
-call
-pop-modelview-matrix
-pop-color ;
+  push-modelview-matrix
+  push-color
+  call
+  pop-modelview-matrix
+  pop-color ; inline
 
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 


: adjust ( val num -- val ) dup 0 > [ 1 pick - * + ] [ dupd * + ] if ;

: saturation ( num -- ) color> dup $saturation rot adjust >>saturation drop ;

: sat ( num -- ) saturation ;

: brightness ( num -- ) color> dup $brightness rot adjust >>brightness drop ;

: b ( num -- ) brightness ;

: alpha ( num -- ) color> dup $alpha rot adjust >>alpha drop ;

: a ( num -- ) alpha ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


USING: kernel alien.c-types combinators namespaces arrays
       sequences sequences.lib namespaces.lib splitting
       math math.functions math.vectors math.trig
       opengl.gl opengl.glu opengl ui ui.gadgets.slate
       combinators.lib vars
       random-weighted colors.hsv cfdg.gl ;

IN: cfdg

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

! hsba { hue saturation brightness alpha }

: <hsba> 4array ;

VAR: color

! ( -- val )

: hue>>        0 color> nth ;
: saturation>> 1 color> nth ;
: brightness>> 2 color> nth ;
: alpha>>      3 color> nth ;

! ( val -- )

: >>hue        0 color> set-nth ;
: >>saturation 1 color> set-nth ;
: >>brightness 2 color> set-nth ;
: >>alpha      3 color> set-nth ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

: hsva>rgba ( hsva -- rgba ) [ 3 head hsv>rgb ] [ peek ] bi add ;

: gl-set-hsba ( hsva -- ) hsva>rgba gl-color ;

: gl-clear-hsba ( hsva -- ) hsva>rgba gl-clear ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

! if (adjustment < 0)
!   base + base * adjustment
