Colormap.st
author ca
Thu, 30 May 1996 16:35:03 +0200
changeset 773 a06487b33240
parent 665 843bf13683f5
child 808 f548a3c6ca8c
permissions -rw-r--r--
checkin from browser

"
 COPYRIGHT (c) 1994 by Claus Gittinger
	      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.
"

SequenceableCollection subclass:#Colormap
	instanceVariableNames:'redVector greenVector blueVector'
	classVariableNames:''
	poolDictionaries:''
	category:'Graphics-Images-Support'
!

!Colormap class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1994 by Claus Gittinger
	      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.
"
!

documentation
"
    Colormaps are used with images (and Forms) to keep the byte-to-color
    mapping.
    Externally, either colors or pixel values can be accessed.
    Internally, the values are stored as 3 separate byte-arrays
    (i.e. individual components can be 0..255).
    This was done to avoid overhead due to allocation of many color
    instances.


    [author:]
        Claus Gittinger

    [see also:]
        Color Image Form
"
! !

!Colormap class methodsFor:'instance creation'!

fromColors:aColorArray
    "given a collection of real colors, return a new instance
     of myself"

    |n rV gV bV|

    n := aColorArray size.
    rV := ByteArray new:n.
    gV := ByteArray new:n.
    bV := ByteArray new:n.
    1 to:n do:[:i |
        |clr|

        clr := aColorArray at:i.
        rV at:i put:(clr red * 255 / 100) rounded.
        bV at:i put:(clr green * 255 / 100) rounded.
        gV at:i put:(clr blue * 255 / 100) rounded.
    ].
    ^ self new redVector:rV greenVector:gV blueVector:bV

    "
     Colormap 
        fromColors:(Array with:Color black
                          with:Color red
                          with:Color white)
    "

    "Modified: 23.4.1996 / 22:15:22 / cg"
!

redVector:r greenVector:g blueVector:b
    "given vectors of red/green/and blue pixelValues,
     return a new instance of myself.
     The values must be in the range 0..255."

    ^ self new redVector:r greenVector:g blueVector:b

    "
     Colormap 
        redVector:#[0 127 255]
        greenVector:#[0 127 255]
        blueVector:#[0 127 255]
    "

    "Modified: 23.4.1996 / 22:16:00 / cg"
! !

!Colormap methodsFor:'accessing'!

at:index 
    "return the color for a index. 
     Notice that index range is 1..."

    |r g b idx "{ Class: SmallInteger }" |

    idx := index.
    r := redVector at:idx.
    g := greenVector at:idx.
    b := blueVector at:idx.

    ^ Color
        red:(r * 100 / 255)
        green:(g * 100 / 255)
        blue:(b * 100 / 255)

    "Created: 2.5.1996 / 12:21:40 / cg"
    "Modified: 2.5.1996 / 17:29:24 / cg"
!

at:index put:aColor
    "set the color for a index. Return aColor (sigh).
     Notice that index range is 1..."

    |r g b idx "{ Class: SmallInteger }" |

    r := (aColor red * 255 / 100) rounded.
    g := (aColor green * 255 / 100) rounded.
    b := (aColor blue * 255 / 100) rounded.

    idx := index.
    redVector at:idx put:r.
    greenVector at:idx put:g.
    blueVector at:idx put:b.
    ^ aColor

    "Modified: 2.5.1996 / 17:29:29 / cg"
!

blueAt:index
    "return the blue component for some index.
     The returned value is scaled from the internal 0.. 255 to
     a percentage value 0..100"

    ^ (blueVector at:index) * 100 / 255

    "Modified: 2.5.1996 / 17:29:17 / cg"
!

colors
    "ST-80 compatibility: return a collection containing the colors I hold"

    ^ self asArray
!

greenAt:index
    "return the green component for some index.
     The returned value is scaled from the internal 0.. 255 to
     a percentage value 0..100"

    ^ (greenVector at:index) * 100 / 255

    "Modified: 2.5.1996 / 17:29:36 / cg"
!

redAt:index
    "return the red component for some index.
     The returned value is scaled from the internal 0.. 255 to
     a percentage value 0..100"

    ^ (redVector at:index) * 100 / 255

    "Modified: 2.5.1996 / 17:29:44 / cg"
!

size
    "return the number of colors in the receiver"

    ^ redVector size

    "Modified: 23.4.1996 / 22:13:43 / cg"
! !

!Colormap methodsFor:'accessing - internals'!

blueVector
    "return the blueVector"

    ^ blueVector

    "Modified: 2.5.1996 / 12:44:17 / cg"
!

blueVector:something
    "set blueVector"

    blueVector := something.
!

greenVector
    "return greenVector"

    ^ greenVector
!

greenVector:something
    "set greenVector"

    greenVector := something.
!

redVector
    "return redVector"

    ^ redVector
!

redVector:something
    "set redVector"

    redVector := something.
!

redVector:r greenVector:g blueVector:b
    "set the red, green and blueVectors"

    redVector := r.
    greenVector := g.
    blueVector := b.

    "Modified: 23.4.1996 / 22:13:31 / cg"
! !

!Colormap methodsFor:'misc'!

scaleValuesBy:scaleFactor
    "multiply all values by scaleFactor; finally round to integer.
     This can be used to brighten/darken an images colormap"

    |sz "{ Class: SmallInteger }" |

    sz := redVector size.
    1 to:sz do:[:index |
        redVector at:index put:((redVector at:index) * scaleFactor) rounded.
        greenVector at:index put:((greenVector at:index) * scaleFactor) rounded.
        blueVector at:index put:((blueVector at:index) * scaleFactor) rounded.
    ]

    "Modified: 2.5.1996 / 12:46:07 / cg"
! !

!Colormap methodsFor:'storing'!

storeOn:aStream
    "append a representation to aStream, from which the receiver
     can be reconstructed"

    aStream nextPutAll:'(' , self class name ,' redVector:'.
    redVector storeOn:aStream.
    aStream nextPutAll:' greenVector:'.
    greenVector storeOn:aStream.
    aStream nextPutAll:' blueVector:'.
    blueVector storeOn:aStream.
    aStream nextPutAll:')'

    "Created: 30.5.1996 / 16:28:27 / ca"
    "Modified: 30.5.1996 / 16:32:44 / ca"
! !

!Colormap class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview/Colormap.st,v 1.14 1996-05-30 14:35:03 ca Exp $'
! !