TranslucentColor.st
author Claus Gittinger <cg@exept.de>
Wed, 22 Aug 2018 12:58:11 +0200
changeset 8451 6eafe0433763
parent 8366 ceaa8f7035a5
child 8715 4bacf77e4211
permissions -rw-r--r--
#QUALITY by cg class: WindowSensor comment/format in: #basicAddDamage:view:

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 1999 by Claus Gittinger / 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 }"

Color subclass:#TranslucentColor
	instanceVariableNames:'alpha'
	classVariableNames:''
	poolDictionaries:''
	category:'Graphics-Support'
!

!TranslucentColor class methodsFor:'documentation'!

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

documentation
"
    TranslucentColor represents colors with an alpha (transparency) channel.
    This is experimental and not yet used by the system.

    [Instance variables:]

      alpha           <Integer>       the internal alpha value (0..255)
"
! !

!TranslucentColor class methodsFor:'instance creation'!

red:r green:g blue:b alpha:alpha
    alpha = 1 ifTrue:[
	^ super red:r green:g blue:b
    ].
    ^ (super
	   scaledRed:(r * MaxValue // 100)
	   scaledGreen:(g * MaxValue // 100)
	   scaledBlue:(b * MaxValue // 100)) alpha:alpha

!

scaledRed:r scaledGreen:g scaledBlue:b alpha:alpha
    ^ self new 
           setScaledRed:r
           scaledGreen:g
           scaledBlue:b
           alpha:alpha

    "
     TranslucentColor scaledRed:0 scaledGreen:0 scaledBlue:0
     TranslucentColor scaledRed:0 scaledGreen:0 scaledBlue:0 alpha:1
     TranslucentColor scaledRed:0 scaledGreen:0 scaledBlue:0 alpha:0.5
     (TranslucentColor scaledRed:0 scaledGreen:0 scaledBlue:0 alpha:0.5) lightened
    "
! !

!TranslucentColor methodsFor:'accessing'!

alpha
    "return the alpha value (0..1),
     where 0 is completely transparent and 1 is completely opaque"

    ^ alpha asFloat / 255.0


!

alpha:alphaFraction
    "set the alpha value (0..1),
     where 0 is completely transparent and 1 is completely opaque"

    "/ self assert:(alphaFraction between:0 and:1).
    alpha := (alphaFraction * 255) rounded

    "Modified: / 05-09-2017 / 16:46:11 / cg"
!

alphaByte
    "return the alpha value as byte 0..255,
     where 0 is completely transparent and 255 is completely opaque"

    ^ alpha ? 255
!

privateAlpha
    "return the internal alpha value (0..255),
     where 0 is completely transparent and 255 is completely opaque"

    ^ alpha
!

scaledAlpha
    "return the alpha value (0..16rFFFF),
     where 0 is completely transparent and 16rFFFF is completely opaque"

    ^ alpha * 16rFFFF // 255
!

setAlphaByte:aByteValuedInteger
    "set the alpha value (0..255),
     where 0 is completely transparent and 255 is completely opaque"

    alpha := aByteValuedInteger


!

setScaledRed:r scaledGreen:g scaledBlue:b
    "r,g,b must be 0..MAXVALUE"

    super setScaledRed:r scaledGreen:g scaledBlue:b.
    alpha isNil ifTrue:[
        alpha := 255 "/ opaque
    ].

    "
     self scaledRed:0 scaledGreen:0 scaledBlue:0
    "
!

setScaledRed:r scaledGreen:g scaledBlue:b alpha:alphaFraction
    "alphaFraction must be 0..1;
     r,g,b must be 0..MAXVALUE"

    self setScaledRed:r scaledGreen:g scaledBlue:b.
    self alpha:alphaFraction.

    "
     self scaledRed:0 scaledGreen:0 scaledBlue:0 alpha:0.5
    "
!

setScaledRed:r scaledGreen:g scaledBlue:b alphaByte:a
    "a must be 0..255;
     r,g,b must be 0..MAXVALUE"

    self setScaledRed:r scaledGreen:g scaledBlue:b .
    self setAlphaByte:a.

    "
     self scaledRed:0 scaledGreen:0 scaledBlue:0 alpha:1
    "
! !

!TranslucentColor methodsFor:'color operations'!

mixed:amount with:aColor
    "create a new color from mixing amount of the receiver
     with the argument, aColor.
     Mixing is done by adding components (i.e. additive mixing)
     (which is different from mixing colors on paper, which is subtractive).
     With an amount of 1, this is the same as blendWith."

    |newColor|

    newColor := super mixed:amount with:aColor.
    newColor setAlphaByte:(self alphaByte).
    ^ newColor

    "
     TranslucentColor red:50 green:50 blue:50 alpha:0.5
     (TranslucentColor red:50 green:50 blue:50 alpha:0.5) lightened
     (TranslucentColor red:50 green:50 blue:50 alpha:0.5) darkened
    "
! !

!TranslucentColor methodsFor:'comparing'!

= aColor
    ^ aColor species == self species 
        and:[(self alpha = aColor alpha) and:[super = aColor]].

    "Modified: / 17-05-2018 / 14:53:34 / Stefan Vogel"
!

almostSameAs:aColor
    ^ (self alpha = aColor alpha)
      and:[ super almostSameAs:aColor ]
! !

!TranslucentColor methodsFor:'inspecting'!

inspectorValueStringInListFor:anInspector
    "returns a string to be shown in the inspector's list"

    ^ self htmlPrintString,'.',(self alphaByte hexPrintString:2)

    "Created: / 05-09-2017 / 10:43:54 / cg"
! !

!TranslucentColor methodsFor:'printing & storing'!

storeOn:aStream
    "append a string representing an expression to reconstruct the receiver
     to the argument, aStream"

    |clsName|

    clsName := self class name.

    aStream nextPutAll:'(' , clsName , ' red:'.
    (self red) storeOn:aStream.
    aStream nextPutAll:' green:'.
    (self green) storeOn:aStream.
    aStream nextPutAll:' blue:'.
    (self blue) storeOn:aStream.
    aStream nextPutAll:' alpha:'.
    (self alpha) storeOn:aStream.
    aStream nextPut:$).

    "
     (self red:100 green:100 blue:0 alpha:1) storeOn:Transcript
    "
! !

!TranslucentColor methodsFor:'queries'!

isOpaque
    "return true, if I represent an opaque color"

    ^ alpha == 255

!

isTranslucent
    "return true, if I represent a translucent color;
     that is: not completely opaque"

    ^ alpha < 255

!

isTranslucentColor
    "return true, if I represent a translucent color;
     This means: self isTranslucent, but isTransparent not"

    ^ alpha > 0
!

isTransparent
    "return true, if I represent a completely transparent color"

    ^ alpha == 0
! !

!TranslucentColor class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !