GradientBackground.st
author Claus Gittinger <cg@exept.de>
Thu, 24 Mar 2011 09:59:57 +0100
changeset 5733 08e2b386abef
parent 5732 fba4327a852b
child 5738 b69308a38fb6
permissions -rw-r--r--
changed: #fillRectangleX:y:width:height:in: start-color fix; tuned by drawing rectangles.

"
 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' }"

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
"
    |bg v|

    bg := GradientBackground new color1:Color red color2:Color yellow.
    v := View new extent:300@300.
    v viewBackground:bg.
    v open.
"
! !

!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

    "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"
! !

!GradientBackground methodsFor:'converting'!

asFormOn:aDevice
    |h w|

    cachedForm isNil ifTrue:[
        (direction == #northSouth or:[direction == nil]) ifTrue:[
            h := usedLength ? (Display height). "/ aView height.
            w := 8.
        ] ifFalse:[
            h := 8.
            w := usedLength ? (Display 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 
     yRect hRun xRect wRun 
     rC gC bC lastR lastG lastB 
     minR maxR minG maxG minB maxB|

    "/ always take the full-screen as reference
    "/ (so we do not have to care for changed gradient, when view changes size)
    hAll := usedLength ? (Display height). "/ aView height.
    wAll := usedLength ? (Display 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 * y).
        dG := (g2 - g1) / hAll.
        g := g1 + (dG * y).
        dB := (b2 - b1) / hAll.
        b := b1 + (dB * y).

        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 * x).
        dG := (g2 - g1) / wAll.
        g := g1 + (dG * x).
        dB := (b2 - b1) / wAll.
        b := b1 + (dB * x).

        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: / 24-03-2011 / 09:59:20 / cg"
! !

!GradientBackground class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libview/GradientBackground.st,v 1.11 2011-03-24 08:59:57 cg Exp $'
! !