Skip to content
This repository was archived by the owner on Oct 28, 2018. It is now read-only.

Commit

Permalink
Hema field validation (Closes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Trévisan committed Mar 10, 2015
1 parent ec88b03 commit eafd72e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Binary file not shown.
31 changes: 25 additions & 6 deletions Colour Contrast Analyser/ColorController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Cocoa

class ColorController: NSViewController {
class ColorController: NSViewController, NSTextFieldDelegate {
var color: NSColor!
@IBOutlet weak var redSlider: NSSlider!
@IBOutlet weak var greenSlider: NSSlider!
Expand All @@ -30,6 +30,20 @@ class ColorController: NSViewController {
self.updateSliders()
self.updateRGB()
self.updatePreview()
hexField.delegate = self
}

override func controlTextDidChange(obj: NSNotification) {
var correctedString:NSString = hexField.stringValue
//Trims non-numerical characters
var regex = NSRegularExpression(pattern: "[^0-9AaBbCcDdEeFf]", options: nil, error: nil)
correctedString = regex!.stringByReplacingMatchesInString(correctedString, options:nil, range:NSMakeRange(0, correctedString.length), withTemplate:"")
// Length 6
if correctedString.length > 6 {
correctedString = correctedString.substringToIndex(6)
}
correctedString = correctedString.uppercaseString
hexField.stringValue = "#" + correctedString
}

@IBAction func sliderChanged(sender: NSSlider) {
Expand All @@ -50,11 +64,16 @@ class ColorController: NSViewController {
}

@IBAction func hexChanged(sender: NSTextField) {
self.color = NSColor(hexString: sender.stringValue)
self.updatePreview()
self.updateSliders()
self.updateRGB()
self.sendNotification()
if countElements(sender.stringValue) == 7 {
hexField.backgroundColor = NSColor.whiteColor()
self.color = NSColor(hexString: sender.stringValue)
self.updatePreview()
self.updateSliders()
self.updateRGB()
self.sendNotification()
} else {
hexField.backgroundColor = NSColor.redColor()
}
}

@IBAction func rgbChanged(sender: NSTextField) {
Expand Down

0 comments on commit eafd72e

Please sign in to comment.