Color.st
changeset 8243 c16d146a30e1
parent 8171 17c30710ed4a
child 8277 942b74deecfe
equal deleted inserted replaced
8242:5ca21d0d739d 8243:c16d146a30e1
   853      The value is composed of r<<16 + g<<8 + b,
   853      The value is composed of r<<16 + g<<8 + b,
   854      i.e. rrggbb"
   854      i.e. rrggbb"
   855 
   855 
   856     |r g b|
   856     |r g b|
   857 
   857 
       
   858     "/ constant colors
       
   859     rgb == 0 ifTrue:[
       
   860         ^ self black
       
   861     ].
       
   862     rgb == 16rFFFFFF ifTrue:[
       
   863         ^ self white
       
   864     ].
       
   865 
   858     r := (rgb bitShift:-16) bitAnd:16rFF.
   866     r := (rgb bitShift:-16) bitAnd:16rFF.
   859     g := (rgb bitShift:-8) bitAnd:16rFF.
   867     g := (rgb bitShift:-8) bitAnd:16rFF.
   860     b := (rgb) bitAnd:16rFF.
   868     b := rgb bitAnd:16rFF.
   861     ^ self redByte:r greenByte:g blueByte:b
   869     ^ self redByte:r greenByte:g blueByte:b
   862 
   870 
   863     "
   871     "
   864      (Color rgbValue:16rFF0000) inspect
   872      (Color rgbValue:16rFF0000) inspect
   865      (Color rgbValue:16r00FF00) inspect
   873      (Color rgbValue:16r00FF00) inspect
   866      (Color rgbValue:16r0000FF) inspect
   874      (Color rgbValue:16r0000FF) inspect
   867      (Color rgbValue:16rFF00FF) inspect
   875      (Color rgbValue:16rFF00FF) inspect
       
   876      (Color rgbValue:16rFFFFFF) inspect
   868     "
   877     "
   869 
   878 
   870     "Modified: / 13-08-1997 / 20:24:37 / cg"
   879     "Modified: / 13-08-1997 / 20:24:37 / cg"
   871     "Modified (comment): / 26-08-2017 / 13:02:23 / cg"
   880     "Modified (comment): / 26-08-2017 / 13:02:23 / cg"
       
   881     "Modified (format): / 11-01-2018 / 12:21:56 / stefan"
   872 !
   882 !
   873 
   883 
   874 scaledGray:aGrayValue
   884 scaledGray:aGrayValue
   875     "return a gray color with a scaled gray value (0..MaxValue)"
   885     "return a gray color with a scaled gray value (0..MaxValue)"
   876 
   886 
  5125         (#Color constantColorSymbol)
  5135         (#Color constantColorSymbol)
  5126      This is the reverse operation to #literalArrayEncoding."
  5136      This is the reverse operation to #literalArrayEncoding."
  5127 
  5137 
  5128     |clr nameOrRGB|
  5138     |clr nameOrRGB|
  5129 
  5139 
       
  5140     red notNil ifTrue:[
       
  5141         "oops cannot change an existing color (you want to make red be green - or what)"
       
  5142         self error:'Colors cannot change their components'.
       
  5143         ^ self
       
  5144     ].
       
  5145 
  5130     encoding size == 2 ifTrue:[
  5146     encoding size == 2 ifTrue:[
  5131         nameOrRGB := encoding at:2.
  5147         nameOrRGB := encoding at:2.
  5132         nameOrRGB isSymbol ifTrue:[
  5148         nameOrRGB isSymbol ifTrue:[
  5133             clr := self class perform:nameOrRGB
  5149             clr := self class name:nameOrRGB
  5134         ] ifFalse:[
  5150         ] ifFalse:[
  5135             clr := self class rgbValue:nameOrRGB.
  5151             clr := self class rgbValue:nameOrRGB.
  5136         ].
  5152         ].
  5137         red := clr scaledRed.
  5153         red := clr scaledRed.
  5138         green := clr scaledGreen.
  5154         green := clr scaledGreen.
  5139         blue := clr scaledBlue
  5155         blue := clr scaledBlue
  5140     ] ifFalse:[
  5156     ] ifFalse:[
  5141         red := ((encoding at:2) / 100.0 * MaxValue) rounded asInteger.
  5157         red := ((encoding at:2) / 100.0 * MaxValue) rounded.
  5142         green := ((encoding at:3) / 100.0 * MaxValue) rounded asInteger.
  5158         green := ((encoding at:3) / 100.0 * MaxValue) rounded.
  5143         blue := ((encoding at:4) / 100.0 * MaxValue) rounded asInteger.
  5159         blue := ((encoding at:4) / 100.0 * MaxValue) rounded.
  5144     ].
  5160     ].
  5145 
  5161 
  5146     "
  5162     "
  5147       Color new fromLiteralArrayEncoding:#(#Color 50 25 25)
  5163       Color new fromLiteralArrayEncoding:#(Color 50 25 25)
  5148       Color new fromLiteralArrayEncoding:#(#Color 16rFF00FF)
  5164       Color new fromLiteralArrayEncoding:#(Color 16rFF00FF)
  5149       Color new fromLiteralArrayEncoding:#(#Color blue)
  5165       Color new fromLiteralArrayEncoding:#(Color blue)
  5150     "
  5166     "
       
  5167 
       
  5168     "Modified (comment): / 11-01-2018 / 12:42:56 / stefan"
  5151 !
  5169 !
  5152 
  5170 
  5153 literalArrayEncoding
  5171 literalArrayEncoding
  5154     "encode myself as an array, from which a copy of the receiver
  5172     "encode myself as an array, from which a copy of the receiver
  5155      can be reconstructed with #decodeAsLiteralArray.
  5173      can be reconstructed with #decodeAsLiteralArray.
  6027 
  6045 
  6028 setScaledRed:r scaledGreen:g scaledBlue:b device:aDevice
  6046 setScaledRed:r scaledGreen:g scaledBlue:b device:aDevice
  6029     "private: set the components"
  6047     "private: set the components"
  6030 
  6048 
  6031     red notNil ifTrue:[
  6049     red notNil ifTrue:[
  6032 	"oops cannot change (you want to make red be green - or what)"
  6050         "oops cannot change an existing color (you want to make red be green - or what)"
  6033 
  6051         self error:'Colors cannot change their components'.
  6034 	self error:'Colors cannot change their components'.
  6052         ^ self
  6035 	^ self
       
  6036     ].
  6053     ].
  6037     red := r.
  6054     red := r.
  6038     green := g.
  6055     green := g.
  6039     blue := b.
  6056     blue := b.
  6040     device := aDevice
  6057     device := aDevice
  6041 
  6058 
  6042     "Modified: 16.1.1997 / 22:39:26 / cg"
  6059     "Modified: / 16-01-1997 / 22:39:26 / cg"
       
  6060     "Modified (format): / 11-01-2018 / 12:25:46 / stefan"
  6043 ! !
  6061 ! !
  6044 
  6062 
  6045 !Color methodsFor:'queries'!
  6063 !Color methodsFor:'queries'!
  6046 
  6064 
  6047 averageColor
  6065 averageColor