#FEATURE
class: GradientBackground
added:
#needsFullRedrawOnChangeOfHeight
#needsFullRedrawOnChangeOfWidth
comment/format in: #examples
changed:
#asFormOn:
#direction
#fillRectangleX:y:width:height:in:
"{ Encoding: utf8 }"
"
COPYRIGHT (c) 2009 by eXept Software AG
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
"{ Package: 'stx:libview' }"
"{ NameSpace: Smalltalk }"
AbstractBackground subclass:#GradientBackground
instanceVariableNames:'color1 color2 direction cachedForm usedLength'
classVariableNames:''
poolDictionaries:''
category:'Graphics-Support'
!
!GradientBackground class methodsFor:'documentation'!
copyright
"
COPYRIGHT (c) 2009 by eXept Software AG
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
"
!
examples
"
[exBegin]
|bg v|
bg := GradientBackground new color1:Color red color2:Color yellow.
v := View new extent:300@300.
v viewBackground:bg.
v open.
[exEnd]
[exBegin]
|bg v|
bg := GradientBackground new color1:Color red color2:Color yellow.
bg usedLength:100.
v := View new extent:300@300.
v viewBackground:bg.
v open.
[exEnd]
[exBegin]
|bg v|
bg := GradientBackground new color1:Color red color2:Color yellow.
bg usedLength:#full.
v := View new extent:300@300.
v viewBackground:bg.
v open.
[exEnd]
"
! !
!GradientBackground class methodsFor:'instance creation'!
horizontal:color1 to:color2
^ self new
direction:#eastWest;
color1:color1 color2:color2
"
|bg v|
bg := GradientBackground horizontal:Color red to:Color yellow.
v := View new extent:300@300.
v viewBackground:bg.
v open.
"
"
|bg v|
bg := GradientBackground horizontal:Color red to:Color yellow.
v := View new extent:300@300.
v viewBackground:bg.
v open.
"
!
vertical:color1 to:color2
^ self new
direction:#northSouth;
color1:color1 color2:color2
"
|bg v|
bg := GradientBackground vertical:Color red to:Color yellow.
v := View new extent:300@300.
v viewBackground:bg.
v open.
"
! !
!GradientBackground class methodsFor:'queries'!
possibleDirections
^ #(#'northSouth' #'eastWest' )
! !
!GradientBackground methodsFor:'accessing'!
color1
^ color1
"Created: / 23-01-2011 / 02:01:29 / cg"
!
color1:aColor
color1 := aColor.
cachedForm := nil.
"Created: / 23-01-2011 / 02:01:50 / cg"
"Modified: / 03-02-2011 / 19:49:18 / cg"
!
color1:aColor1 color2:aColor2
color1 := aColor1.
color2 := aColor2.
cachedForm := nil.
"Created: / 03-02-2011 / 19:52:59 / cg"
!
color2
^ color2
"Created: / 23-01-2011 / 02:01:32 / cg"
!
color2:aColor
color2 := aColor.
cachedForm := nil.
"Created: / 23-01-2011 / 02:01:55 / cg"
"Modified: / 03-02-2011 / 19:49:23 / cg"
!
direction
"possible values:
#northSouth
#eastWest
others are not yet supported
"
^ direction ? #northSouth
"Modified: / 23-01-2011 / 14:36:36 / cg"
!
direction:something
"possible values:
#northSouth
#eastWest
others are not yet supported
"
direction ~= something ifTrue:[
direction := something.
cachedForm := nil.
]
"Modified: / 03-02-2011 / 19:49:41 / cg"
!
usedLength
^ usedLength
!
usedLength:something
usedLength := something.
! !
!GradientBackground methodsFor:'converting'!
asFormOn:aDevice
|h w len|
"/ usedLength == #full ifTrue:[
"/ self halt:'unsupported'.
"/ ^ nil
"/ ].
cachedForm isNil ifTrue:[
(len := usedLength) == #full ifTrue:[
len := nil
].
(direction == #northSouth or:[direction == nil]) ifTrue:[
h := len ? (aDevice height). "/ aView height.
w := 8.
] ifFalse:[
h := 8.
w := len ? (aDevice width). "/ aView width.
].
cachedForm := Form width:w height:h depth:aDevice depth onDevice:aDevice.
"/ cachedForm clear.
self fillRectangleX:0 y:0 width:w height:h in:cachedForm
].
^ cachedForm
"Created: / 03-02-2011 / 20:05:30 / cg"
!
onDevice:device
^ self asFormOn:device.
"Created: / 03-02-2011 / 19:56:06 / cg"
! !
!GradientBackground methodsFor:'drawing'!
fillRectangleX:x y:y width:w height:h in:aView
"this is a first (very inefficient) try"
|hAll wAll r1 r2 g1 g2 b1 b2 dR r dG g dB b scaleStartX scaleStartY
yRect hRun xRect wRun
rC gC bC lastR lastG lastB
minR maxR minG maxG minB maxB|
scaleStartX := x.
scaleStartY := y.
"/ always take the full-screen as reference
"/ (so we do not have to care for changed gradient, when view changes size)
usedLength notNil ifTrue:[
usedLength == #full ifTrue:[
hAll := aView height.
wAll := aView width.
"/ scaleStartX := 0.
"/ scaleStartY := 0.
] ifFalse:[
hAll := usedLength.
wAll := usedLength.
"/ scaleStartX := 0.
"/ scaleStartY := 0.
]
] ifFalse:[
hAll := aView device height. "/ aView height.
wAll := aView device width. "/ aView width.
].
r1 := color1 redByte.
r2 := color2 redByte.
g1 := color1 greenByte.
g2 := color2 greenByte.
b1 := color1 blueByte.
b2 := color2 blueByte.
minR := r1 min:r2.
maxR := r1 max:r2.
minG := g1 min:g2.
maxG := g1 max:g2.
minB := b1 min:b2.
maxB := b1 max:b2.
aView lineStyle:#solid.
"/ individual lines; from top to bottom
(direction == #northSouth or:[direction == #vertical or:[direction == nil]]) ifTrue:[
dR := (r2 - r1) / hAll.
r := r1 + (dR * scaleStartY).
dG := (g2 - g1) / hAll.
g := g1 + (dG * scaleStartY).
dB := (b2 - b1) / hAll.
b := b1 + (dB * scaleStartY).
r := (r max:minR) min:maxR.
g := (g max:minG) min:maxG.
b := (b max:minB) min:maxB.
lastR := r asInteger. lastG := g asInteger. lastB := b asInteger.
yRect := y.
hRun := 0.
y to:y+h-1 do:[:yP |
rC := r asInteger.
gC := g asInteger.
bC := b asInteger.
(rC ~~ lastR or:[gC ~~ lastG or:[bC ~~ lastB]]) ifTrue:[
aView paint:(Color redByte:lastR greenByte:lastG blueByte:lastB).
aView fillRectangleX:x y:yRect width:w height:hRun.
yRect := yP. hRun := 0.
lastR := rC. lastG := gC. lastB := bC.
].
hRun := hRun + 1.
r := ((r + dR) max:minR) min:maxR.
g := ((g + dG) max:minG) min:maxG.
b := ((b + dB) max:minB) min:maxB.
].
hRun ~~ 0 ifTrue:[
aView paint:(Color redByte:lastR greenByte:lastG blueByte:lastB).
aView fillRectangleX:x y:yRect width:w height:hRun.
].
^ self.
].
"/ individual lines; from left to right
(direction == #eastWest or:[direction == #horizontal]) ifTrue:[
dR := (r2 - r1) / wAll.
r := r1 + (dR * scaleStartX).
dG := (g2 - g1) / wAll.
g := g1 + (dG * scaleStartX).
dB := (b2 - b1) / wAll.
b := b1 + (dB * scaleStartX).
r := (r max:minR) min:maxR.
g := (g max:minG) min:maxG.
b := (b max:minB) min:maxB.
lastR := r asInteger. lastG := g asInteger. lastB := b asInteger.
xRect := x.
wRun := 0.
x to:x+w-1 do:[:xP |
rC := r asInteger.
gC := g asInteger.
bC := b asInteger.
(rC ~~ lastR or:[gC ~~ lastG or:[bC ~~ lastB]]) ifTrue:[
aView paint:(Color redByte:lastR greenByte:lastG blueByte:lastB).
aView fillRectangleX:xRect y:y width:wRun height:h.
xRect := xP. wRun := 0.
lastR := rC. lastG := gC. lastB := bC.
].
wRun := wRun + 1.
r := ((r + dR) max:minR) min:maxR.
g := ((g + dG) max:minG) min:maxG.
b := ((b + dB) max:minB) min:maxB.
].
wRun ~~ 0 ifTrue:[
aView paint:(Color redByte:lastR greenByte:lastG blueByte:lastB).
aView fillRectangleX:xRect y:y width:wRun height:h.
].
^ self.
]
"Created: / 23-01-2011 / 01:59:29 / cg"
"Modified: / 06-04-2011 / 21:18:07 / cg"
! !
!GradientBackground methodsFor:'queries'!
needsFullRedrawOnChangeOfHeight
^ usedLength == #full and:[self direction == #northSouth]
!
needsFullRedrawOnChangeOfWidth
^ usedLength == #full and:[direction == #eastWest]
! !
!GradientBackground class methodsFor:'documentation'!
version
^ '$Header$'
!
version_CVS
^ '$Header$'
! !