home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / SMALTALK / TEXTBOOK / AP05.ST (.txt) < prev    next >
Text File  |  1997-04-22  |  20KB  |  642 lines

  1.  
  2. 'Smalltalk Textbook Appendix 05'!
  3.  
  4.  
  5.  
  6.  
  7.  
  8. EngiVariable subclass: #EngiColorModel
  9.     instanceVariableNames: ''
  10.     classVariableNames: 'MenuOfColorConstants MenuOfYellowButton RecentSelectedColor '
  11.     poolDictionaries: ''
  12.     category: 'Engi-Color'!
  13. EngiColorModel comment:
  14. '
  15.  
  16. Engi 0.01 (10 January 1994)
  17. Copyright (C) 1994 by Atsushi Aoki
  18.  
  19. '!
  20.  
  21.  
  22. !EngiColorModel methodsFor: 'initialize-release'!
  23.  
  24. initialize
  25.     self setValue: (ColorValue
  26.             hue: 0
  27.             saturation: 0
  28.             brightness: 1).
  29.     ^self! !
  30.  
  31. !EngiColorModel methodsFor: 'accessing'!
  32.  
  33. brightness
  34.     ^self value brightness!
  35.  
  36. brightness: brightnessValue 
  37.     | brightness |
  38.     brightness := 0 max: (brightnessValue min: 1).
  39.     self setValue: (ColorValue
  40.             hue: self hue
  41.             saturation: self saturation
  42.             brightness: brightness).
  43.     brightness = 0
  44.         ifTrue: 
  45.             [self changed: #hue.
  46.             self changed: #saturation].
  47.     self changed: #brightness.
  48.     self changed: #color!
  49.  
  50. hue
  51.     ^self value hue!
  52.  
  53. hue: hueValue 
  54.     | hue |
  55.     hue := 0 max: (hueValue min: 1).
  56.     self setValue: (ColorValue
  57.             hue: hue
  58.             saturation: self saturation
  59.             brightness: self brightness).
  60.     self changed: #hue.
  61.     self changed: #sturation.
  62.     self changed: #color!
  63.  
  64. hue: hueValue saturation: saturationValue brightness: brightnessValue 
  65.     | hue saturation brightness |
  66.     hue := 0 max: (hueValue min: 1).
  67.     saturation := 0 max: (saturationValue min: 1).
  68.     brightness := 0 max: (brightnessValue min: 1).
  69.     self setValue: (ColorValue
  70.             hue: hue
  71.             saturation: saturation
  72.             brightness: brightness).
  73.     self changed: #hue.
  74.     self changed: #saturation.
  75.     self changed: #brightness.
  76.     self changed: #color!
  77.  
  78. saturation
  79.     ^self value saturation!
  80.  
  81. saturation: saturationValue 
  82.     | saturation |
  83.     saturation := 0 max: (saturationValue min: 1).
  84.     self setValue: (ColorValue
  85.             hue: self hue
  86.             saturation: saturation
  87.             brightness: self brightness).
  88.     saturation = 0 ifTrue: [self changed: #hue].
  89.     self changed: #saturation.
  90.     self changed: #color!
  91.  
  92. value: colorValue 
  93.     colorValue isNil ifFalse: [self
  94.             hue: colorValue hue
  95.             saturation: colorValue saturation
  96.             brightness: colorValue brightness]! !
  97.  
  98. !EngiColorModel methodsFor: 'viewing'!
  99.  
  100. open
  101.     | colorView edgeDecorator topWindow |
  102.     colorView := EngiColorView on: self.
  103.     edgeDecorator := LookPreferences edgeDecorator on: colorView.
  104.     edgeDecorator noMenuBar.
  105.     edgeDecorator noVerticalScrollBar.
  106.     edgeDecorator noHorizontalScrollBar.
  107.     topWindow := EngiTopView
  108.                 model: nil
  109.                 label: 'Color'
  110.                 minimumSize: 300 @ 100.
  111.     topWindow add: edgeDecorator in: (0 @ 0 corner: 1 @ 1).
  112.     topWindow open! !
  113.  
  114. !EngiColorModel methodsFor: 'adaptor'!
  115.  
  116. yellowButtonMenu
  117.     "EngiColorModel flushMenus."
  118.  
  119.     | collection aMenu |
  120.     MenuOfYellowButton isNil
  121.         ifTrue: 
  122.             [collection := OrderedCollection new.
  123.             collection add: 'copy' -> #copyColor.
  124.             collection add: 'print' -> #printColor.
  125.             aMenu := EngiMenuMaker fromCollection: collection.
  126.             MenuOfYellowButton := aMenu].
  127.     ^MenuOfYellowButton! !
  128.  
  129. !EngiColorModel methodsFor: 'menu messages'!
  130.  
  131. copyColor
  132.     ParagraphEditor currentSelection: self printString asText!
  133.  
  134. printColor
  135.     Transcript cr; show: self printString! !
  136.  
  137. !EngiColorModel methodsFor: 'printing'!
  138.  
  139. printOn: aStream 
  140.     aStream nextPutAll: '(ColorValue hue: '.
  141.     aStream nextPutAll: self hue asFloat printString.
  142.     aStream nextPutAll: ' saturation: '.
  143.     aStream nextPutAll: self saturation asFloat printString.
  144.     aStream nextPutAll: ' brightness: '.
  145.     aStream nextPutAll: self brightness asFloat printString.
  146.     aStream nextPutAll: ')'! !
  147. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  148.  
  149. EngiColorModel class
  150.     instanceVariableNames: ''!
  151.  
  152.  
  153. !EngiColorModel class methodsFor: 'class initialization'!
  154.  
  155. flushMenus
  156.     "EngiColorModel flushMenus."
  157.  
  158.     MenuOfColorConstants := nil.
  159.     MenuOfYellowButton := nil! !
  160.  
  161. !EngiColorModel class methodsFor: 'instance creation'!
  162.  
  163. new
  164.     ^super new initialize! !
  165.  
  166. !EngiColorModel class methodsFor: 'view creation'!
  167.  
  168. open
  169.     "EngiColorModel open."
  170.  
  171.     | colorModel |
  172.     colorModel := EngiColorModel new.
  173.     colorModel open.
  174.     ^colorModel!
  175.  
  176. openOn: colorValue 
  177.     "EngiColorModel openOn: ColorValue cyan."
  178.  
  179.     | colorModel |
  180.     colorModel := EngiColorModel new.
  181.     colorModel value: colorValue.
  182.     colorModel open.
  183.     ^colorModel! !
  184.  
  185. !EngiColorModel class methodsFor: 'utilities'!
  186.  
  187. select
  188.     "EngiColorModel select."
  189.  
  190.     ^self select: RecentSelectedColor!
  191.  
  192. select: defaultColorValue 
  193.     "EngiColorModel select: ColorValue cyan."
  194.  
  195.     | colorModel colorView edgeDecorator topView aBoolean |
  196.     colorModel := self new.
  197.     colorModel value: defaultColorValue.
  198.     colorView := EngiColorView on: colorModel.
  199.     edgeDecorator := LookPreferences edgeDecorator on: colorView.
  200.     edgeDecorator noMenuBar.
  201.     edgeDecorator noVerticalScrollBar.
  202.     edgeDecorator noHorizontalScrollBar.
  203.     topView := EngiTopView new.
  204.     topView minimumSize: 300 @ 140.
  205.     topView add: edgeDecorator in: (topView frameFraction: (0 @ 0 corner: 1 @ 1)).
  206.     aBoolean := topView popUp.
  207.     aBoolean
  208.         ifTrue: 
  209.             [RecentSelectedColor := colorModel value.
  210.             ^RecentSelectedColor]
  211.         ifFalse: [^nil]!
  212.  
  213. selectFromColorConstants
  214.     "EngiColorModel selectFromColorConstants."
  215.  
  216.     | colorNames anIndex |
  217.     colorNames := ColorValue constantNames asSortedCollection asArray.
  218.     colorNames isEmpty ifTrue: [^nil].
  219.     MenuOfColorConstants isNil ifTrue: [MenuOfColorConstants := PopUpMenu labelList: (Array with: colorNames)].
  220.     anIndex := MenuOfColorConstants startUp.
  221.     anIndex > 0 ifTrue: [^ColorValue perform: (colorNames at: anIndex)].
  222.     ^nil! !
  223.  
  224. !EngiColorModel class methodsFor: 'examples'!
  225.  
  226. example1
  227.     "EngiColorModel example1."
  228.  
  229.     ^EngiColorModel openOn: ColorValue green!
  230.  
  231. example2
  232.     "EngiColorModel example2."
  233.  
  234.     | colorModel windowCreation |
  235.     colorModel := EngiColorModel new.
  236.     windowCreation := 
  237.             [| colorView edgeDecorator topWindow |
  238.             colorView := EngiColorView on: colorModel.
  239.             edgeDecorator := LookPreferences edgeDecorator on: colorView.
  240.             edgeDecorator noMenuBar.
  241.             edgeDecorator noVerticalScrollBar.
  242.             edgeDecorator noHorizontalScrollBar.
  243.             topWindow := EngiTopView
  244.                         model: nil
  245.                         label: 'Color'
  246.                         minimumSize: 300 @ 100.
  247.             topWindow add: edgeDecorator in: (0 @ 0 corner: 1 @ 1).
  248.             topWindow].
  249.     3 timesRepeat: [windowCreation value open].
  250.     colorModel inspect.
  251.     ^colorModel!
  252.  
  253. example3
  254.     "EngiColorModel example3."
  255.  
  256.     ^EngiColorModel select: ColorValue green! !
  257.  
  258.  
  259.  
  260.  
  261.  
  262. View subclass: #EngiColorView
  263.     instanceVariableNames: 'colorFrame hueFrame saturationFrame brightnessFrame hueImage saturationImage brightnessImage '
  264.     classVariableNames: ''
  265.     poolDictionaries: ''
  266.     category: 'Engi-Color'!
  267. EngiColorView comment:
  268. '
  269.  
  270. Engi 0.01 (10 January 1994)
  271. Copyright (C) 1994 by Atsushi Aoki
  272.  
  273. '!
  274.  
  275.  
  276. !EngiColorView methodsFor: 'controller accessing'!
  277.  
  278. defaultControllerClass
  279.     ^EngiColorController! !
  280.  
  281. !EngiColorView methodsFor: 'bounds accessing'!
  282.  
  283. bounds: newBounds 
  284.     super bounds: newBounds.
  285.     self computeFrames! !
  286.  
  287. !EngiColorView methodsFor: 'controlling'!
  288.  
  289. brightnessActivity
  290.     | aSensor graphicsContext saveCursor oldPoint newPoint insetFrame brightness colorValue |
  291.     aSensor := self controller sensor.
  292.     graphicsContext := self graphicsContext.
  293.     saveCursor := Cursor currentCursor.
  294.     oldPoint := nil.
  295.     [aSensor redButtonPressed and: [brightnessFrame containsPoint: (newPoint := aSensor cursorPoint)]]
  296.         whileTrue: 
  297.             [Cursor currentCursor = Cursor crossHair ifFalse: [Cursor crossHair show].
  298.             newPoint = oldPoint
  299.                 ifFalse: 
  300.                     [insetFrame := brightnessFrame insetBy: 1.
  301.                     brightness := newPoint x - insetFrame origin x / insetFrame width.
  302.                     brightness := 0 max: (brightness min: 1).
  303.                     self displayBrightnessFrameOn: graphicsContext.
  304.                     self displayBrightnessValue: brightness on: graphicsContext.
  305.                     colorValue := ColorValue
  306.                                 hue: self model hue
  307.                                 saturation: self model saturation
  308.                                 brightness: brightness.
  309.                     self displayColorValue: colorValue on: graphicsContext].
  310.             oldPoint := newPoint].
  311.     Cursor currentCursor = saveCursor ifFalse: [saveCursor show].
  312.     (brightnessFrame containsPoint: aSensor cursorPoint)
  313.         ifTrue: [brightness isNil ifFalse: [self model brightness: brightness]]
  314.         ifFalse: 
  315.             [self displayBrightnessFrameOn: graphicsContext.
  316.             self displayBrightnessValueOn: graphicsContext.
  317.             self displayColorFrameOn: graphicsContext.
  318.             self displayColorValueOn: graphicsContext]!
  319.  
  320. colorActivity
  321.     |