GradientBackground.st
author Claus Gittinger <cg@exept.de>
Mon, 31 Jan 2011 17:29:00 +0100
changeset 5692 3187401537b7
parent 5688 0fec2a60ad63
child 5705 56318de85b68
permissions -rw-r--r--
changed: #fillRectangleX:y:width:height:in:

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

!GradientBackground methodsFor:'accessing'!

color1
    ^ color1

    "Created: / 23-01-2011 / 02:01:29 / cg"
!

color1:aColor
    color1 := aColor.

    "Created: / 23-01-2011 / 02:01:50 / cg"
!

color2
    ^ color2

    "Created: / 23-01-2011 / 02:01:32 / cg"
!

color2:aColor
    color2 := aColor.

    "Created: / 23-01-2011 / 02:01:55 / 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.

    "Modified: / 23-01-2011 / 14:36:44 / 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 
     xRight yBot rC gC bC lastR lastG lastB|

    "/ always take the full-screen as reference
    "/ (so we do not have to care for changed gradient, when view changes size)
    hAll := Display height. "/ aView height.
    wAll := Display width.  "/ aView width.

    r1 := color1 redByte.
    r2 := color2 redByte.
    g1 := color1 greenByte.
    g2 := color2 greenByte.
    b1 := color1 blueByte.
    b2 := color2 blueByte.

    "/ individual lines; from top to bottom
    (direction == #northSouth or:[direction == #vertical]) 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).

        xRight := x+w-1.
        0 to:h-1 do:[:yP |
            rC := r asInteger.
            gC := g asInteger.
            bC := b asInteger.
            (rC ~~ lastR or:[gC ~~ lastG or:[bC ~~ lastB]]) ifTrue:[
                aView foreground:(Color redByte:rC greenByte:gC blueByte:bC).
                lastR := rC. lastG := gC. lastB := bC.
            ].
            aView displayLineFromX:x y:y+yP toX:xRight y:y+yP.
            r := (r + dR) min:r2.
            g := (g + dG) min:g2.
            b := (b + dB) min:b2.
        ].
        ^ self.
    ].

    "/ individual lines; from left to right
    (direction == #eastWest or:[direction == #horizontal]) ifTrue:[
        dR := (r2 - r1) / wAll.
        r := r1 + (dR * y).
        dG := (g2 - g1) / wAll.
        g := g1 + (dG * y).
        dB := (b2 - b1) / wAll.
        b := b1 + (dB * y).

        yBot := y+h-1.
        0 to:w-1 do:[:xP |
            rC := r asInteger.
            gC := g asInteger.
            bC := b asInteger.
            (rC ~~ lastR or:[gC ~~ lastG or:[bC ~~ lastB]]) ifTrue:[
                aView foreground:(Color redByte:rC greenByte:gC blueByte:bC).
                lastR := rC. lastG := gC. lastB := bC.
            ].
            aView displayLineFromX:x+xP y:y toX:x+xP y:yBot.
            r := (r + dR) min:r2.
            g := (g + dG) min:g2.
            b := (b + dB) min:b2.
        ].
        ^ self.
    ]

    "Created: / 23-01-2011 / 01:59:29 / cg"
    "Modified: / 31-01-2011 / 17:28:53 / cg"
! !

!GradientBackground class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libview/GradientBackground.st,v 1.5 2011-01-31 16:29:00 cg Exp $'
! !