Skip to content

Instantly share code, notes, and snippets.

@dockimbel
Forked from greggirwin/fill-pen-lab.red
Created June 18, 2016 03:02
Show Gist options
  • Save dockimbel/80016b36d34f1a3973b0e9db18519f42 to your computer and use it in GitHub Desktop.
Save dockimbel/80016b36d34f1a3973b0e9db18519f42 to your computer and use it in GitHub Desktop.
Fill-pen Lab for Red
Red [
Title: "Fill-pen Lab"
Author: "Gregg Irwin"
File: %fill-pen-lab.red
Needs: View
]
to-color: function [r g b][
color: 0.0.0
if r [color/1: to integer! 256 * r]
if g [color/2: to integer! 256 * g]
if b [color/3: to integer! 256 * b]
color
]
to-percent: func [color [integer!]][to percent! (to float! color) / 256]
set-sliders: func [color [tuple!]][
R/data: to-percent color/1
G/data: to-percent color/2
B/data: to-percent color/3
]
to-text: function [val][form to integer! 0.5 + 255 * any [val 0]]
update-field: does [fld-draw-blk/text: mold draw-blk]
xy-to-degree: function [xy [pair!]][
delta: xy - d-circle/:IDX_C_CENTER
radians: atan2 delta/y delta/x
radians * (180 / pi)
]
d-circle: d-fill: none
draw-blk: [
d-pen: pen black
d-fill: fill-pen radial 150x150 0 500 0 1 1 red green blue
d-circle: circle 250x250 250
]
IDX_P_COLOR: 2 ; pen
IDX_C_CENTER: 2 ; circle
IDX_C_RADIUS: 3 ; circle
IDX_F_STYLE: 2 ; fill
IDX_F_OFFSET: 3
IDX_F_START: 4
IDX_F_END: 5
IDX_F_ANGLE: 6 ; angle has no effect for radial gradients
IDX_F_SCALEX: 7
IDX_F_SCALEY: 8
IDX_F_COLOR1: 9
IDX_F_COLOR2: 10
IDX_F_COLOR3: 11
;IDX_F_IMAGE: xxx
value: 0%
;-------------------------------------------------------------------------------
;color-picker: make face! [
; type: 'window text: "Select color" offset: 200x200 size: 200x100
; pane: reduce [
; make face! [type: 'text text: "New window" offset: 10x10 size: 80x20 color: white]
; make face! [
; type: 'button text: "Close" offset: 120x10 size: 60x20
; actors: object [
; on-click: func [face [object!] event [event!]][unview]
; ]
; ]
; ]
;]
;-------------------------------------------------------------------------------
cur-color-face: none
update-cur-color: does [
cur-color-face/color: to-color R/data G/data B/data
]
view [
style txt: text 40 right
style value: text "0" 30 right bold
style color-box: base 50x50 128.128.128
; only buttons get on-click
on-down [cur-color-face: face set-sliders face/color][cur-color-face]
;react [face/color: to-color R/data G/data B/data]
style color-sld: slider 256 value [update-cur-color]
across
txt "Size:" sz-sld: slider 100% return
; drop-down doesn't fire the same events as drop-list
txt "Fill Style:" style-lst: drop-list data ["Radial" "Linear" "Diamond"]
on-create [face/selected: 1]
;on-change [
; d-fill/:IDX_F_STYLE: to word! pick face/data face/selected
;]
return
canvas: base 500x500 draw draw-blk all-over react [
if d-circle [
d-circle/:IDX_C_RADIUS: to integer! 250 * sz-sld/data ; update when sz-sld is moved
]
if d-fill [
d-fill/:IDX_F_STYLE: to word! pick style-lst/data style-lst/selected
d-fill/:IDX_F_COLOR1: C1/color
d-fill/:IDX_F_COLOR2: C2/color
d-fill/:IDX_F_COLOR3: C3/color
]
;face/color: to-color R/data G/data B/data
;print mold draw-blk
; Can't react to the field changing if we are also triggering changes
; from on-down it seems.
;face/draw: load fld-draw-blk/text
]
on-down [
d-fill/:IDX_F_OFFSET: event/offset
update-field
]
; Have to track mouse down/up to make this useful
;on-over [d-fill/:IDX_F_OFFSET: event/offset]
on-alt-down [
; DRAW can't handle floats yet
d-fill/:IDX_F_ANGLE: to integer! xy-to-degree event/offset
update-field
]
return
at 550x25
panel [
text "Click a color box to set the gradient color" return
c1: color-box red c2: color-box green c3: color-box blue return
txt "Red:" R: color-sld react [face/text: to-text R/data] 100% return
txt "Green:" G: color-sld react [face/text: to-text G/data] return
txt "Blue:" B: color-sld react [face/text: to-text B/data] return
pad 0x20
text "Left-click canvas to set gradient origin." return
text "Right-click canvas to set gradient angle." return
text "Draw block: (you can't edit to change values here yet)" return
fld-draw-blk: area 400x200 "" react [
sz-sld/data style-lst/selected
update-field
]
; on-key [
; draw-blk load face/text
; show canvas
; ]
]
do [
cur-color-face: c1
update-field
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment