TranslucentColor.st
changeset 2941 0a6baceacefb
child 2946 5417aafa7ac9
equal deleted inserted replaced
2940:a1ee0883fde2 2941:0a6baceacefb
       
     1 Color subclass:#TranslucentColor
       
     2 	instanceVariableNames:'alpha'
       
     3 	classVariableNames:''
       
     4 	poolDictionaries:''
       
     5 	category:'Graphics-Support'
       
     6 !
       
     7 
       
     8 !TranslucentColor class methodsFor:'documentation'!
       
     9 
       
    10 documentation
       
    11 "
       
    12     TranslucentColor represents colors with an alpha (transparency)
       
    13     channel.
       
    14     This is experimental and not yet used by the system.
       
    15 
       
    16     [Instance variables:]
       
    17 
       
    18       alpha           <Integer>       the alpha value (0..255)
       
    19 "
       
    20 
       
    21 
       
    22 ! !
       
    23 
       
    24 !TranslucentColor class methodsFor:'instance creation'!
       
    25 
       
    26 red:r green:g blue:b alpha:alpha
       
    27     alpha = 1 ifTrue:[
       
    28         ^ super red:r green:g blue:b
       
    29     ].
       
    30     ^ (super 
       
    31            scaledRed:(r * MaxValue // 100)
       
    32            scaledGreen:(g * MaxValue // 100)
       
    33            scaledBlue:(b * MaxValue // 100)) alpha:alpha
       
    34 
       
    35 ! !
       
    36 
       
    37 !TranslucentColor methodsFor:'accessing'!
       
    38 
       
    39 alpha
       
    40     "return the alpha value (0..1),
       
    41      where 0 is completely transparent and 1 is completely opaque"
       
    42 
       
    43     ^ alpha asFloat / 255.0
       
    44 
       
    45 
       
    46 !
       
    47 
       
    48 alpha:alphaFraction
       
    49     "set the alpha value (0..1),
       
    50      where 0 is completely transparent and 1 is completely opaque"
       
    51 
       
    52     alpha := (alphaFraction * 255) rounded
       
    53 
       
    54 
       
    55 !
       
    56 
       
    57 setAlphaByte:aByteValuedInteger
       
    58     "set the alpha value (0..255),
       
    59      where 0 is completely transparent and 255 is completely opaque"
       
    60 
       
    61     alpha := aByteValuedInteger
       
    62 
       
    63 
       
    64 ! !
       
    65 
       
    66 !TranslucentColor methodsFor:'printing & storing'!
       
    67 
       
    68 storeOn:aStream
       
    69     "append a string representing an expression to reconstruct the receiver
       
    70      to the argument, aStream"
       
    71 
       
    72     |clsName|
       
    73 
       
    74     clsName := self class name.
       
    75 
       
    76     aStream nextPutAll:'(' , clsName , ' red:'.
       
    77     (self red) storeOn:aStream.
       
    78     aStream nextPutAll:' green:'.
       
    79     (self green) storeOn:aStream.
       
    80     aStream nextPutAll:' blue:'.
       
    81     (self blue) storeOn:aStream.
       
    82     aStream nextPutAll:' alpha:'.
       
    83     (self alpha) storeOn:aStream.
       
    84     aStream nextPut:$).
       
    85 
       
    86     "
       
    87      (self red:100 green:100 blue:0 alpha:1) storeOn:Transcript
       
    88     "
       
    89 ! !
       
    90 
       
    91 !TranslucentColor methodsFor:'queries'!
       
    92 
       
    93 isOpaque
       
    94     "return true, if I represent an opaque color"
       
    95 
       
    96     ^ alpha == 255
       
    97 
       
    98 !
       
    99 
       
   100 isTranslucent
       
   101     "return true, if I represent a translucent color;
       
   102      that is: not completely opaque"
       
   103 
       
   104     ^ alpha < 255
       
   105 
       
   106 !
       
   107 
       
   108 isTransparent
       
   109     "return true, if I represent a completely transparent color"
       
   110 
       
   111     ^ alpha == 0
       
   112 ! !
       
   113 
       
   114 !TranslucentColor class methodsFor:'documentation'!
       
   115 
       
   116 version
       
   117     ^ '$Header: /cvs/stx/stx/libview/TranslucentColor.st,v 1.1 1999-10-06 16:06:56 cg Exp $'
       
   118 ! !