Skip to content

Commit

Permalink
Fixed text label not setting brightness correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Gofake1 committed May 28, 2017
1 parent dc3d1c5 commit 0ecc11f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Color Picker/ColorController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

import Cocoa

/// Publicly accessible interface of `ColorPickerViewController`; any class can use this class to indirectly
/// Public interface of `ColorPickerViewController`; any class can use this class to indirectly
/// modify `ColorPickerViewController`'s state.
class ColorController {

static var shared = ColorController()
// Used to determine `selectedColor`. Should only be set by `colorPicker`. Other classes should use
// `setColor(_:)`.
// Should only be set by `colorPicker`'s `NSSlider`. Affects `selectedColor`.
var brightness: CGFloat = 1.0 {
didSet {
selectedColor = NSColor(calibratedHue: masterColor.hueComponent,
Expand All @@ -23,6 +22,7 @@ class ColorController {
alpha: 1.0)
}
}
// Should only be set by `colorPicker`'s `ColorWheelView`. Affects `selectedColor`.
var masterColor = NSColor(calibratedRed: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) {
didSet {
selectedColor = NSColor(calibratedHue: masterColor.hueComponent,
Expand All @@ -31,11 +31,11 @@ class ColorController {
alpha: 1.0)
}
}
// Should only be set br `colorPicker`'s `NSTextfield`
var selectedColor = NSColor(calibratedRed: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
// Injected by ColorPickerViewController
weak var colorPicker: ColorPickerViewController!

/// - postcondition: Mutates `colorPicker`
func setColor(_ color: NSColor) {
selectedColor = color
brightness = color.scaledBrightness
Expand Down
11 changes: 3 additions & 8 deletions Color Picker/ColorPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ class ColorPickerViewController: NSViewController {
colorWheelView.setColor(ColorController.shared.selectedColor, crosshairShouldChange)
}

fileprivate func updateLabel(labelTextShouldChange: Bool = true) {
fileprivate func updateLabel() {
colorLabel.backgroundColor = ColorController.shared.selectedColor
if labelTextShouldChange {
colorLabel.stringValue = "#\(ColorController.shared.selectedColor.rgbHexString)"
}
colorLabel.stringValue = "#\(ColorController.shared.selectedColor.rgbHexString)"
if ColorController.shared.selectedColor.scaledBrightness < 0.5 {
colorLabel.textColor = NSColor.white
} else {
Expand Down Expand Up @@ -98,10 +96,7 @@ extension ColorPickerViewController: NSControlTextEditingDelegate {
override func controlTextDidEndEditing(_ obj: Notification) {
let string = (obj.userInfo?["NSFieldEditor"] as! NSTextView).textStorage!.string
let color = NSColor(hexString: string)
ColorController.shared.selectedColor = color
updateColorWheel()
updateLabel(labelTextShouldChange: false)
updateSlider()
ColorController.shared.setColor(color)

view.window?.makeFirstResponder(view)
}
Expand Down
6 changes: 4 additions & 2 deletions Color Picker/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.0.1</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>1.0.1</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.graphics-design</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
Expand Down

0 comments on commit 0ecc11f

Please sign in to comment.