/
DefaultRGBBuilder.swift
92 lines (73 loc) · 3.96 KB
/
DefaultRGBBuilder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// DefaultRGBBuilder.swift
// ColorSenseRainbow
//
// Created by Reid Gravelle on 2015-04-25.
// Copyright (c) 2015 Northern Realities Inc. All rights reserved.
//
import AppKit
class DefaultRGBBuilder: ColorBuilder {
/**
Generates a String containing the code required to create a color object for the specified color in the method detailed by the SearchResults. While a new string could be generated more easily by just entering the values into a template replacing the values in the existing string will keep the formatting the way the user prefers. This involves moving backwards through the values as the new values may be different lengths and would change the ranges for later values.
:param: color The new color that will be described by the string.
:param: forSearchResults A SearchResults object containing the ranges for text to replace.
:returns: A String object containing code how to create the color.
*/
override func stringForColor( color : NSColor, forSearchResult : SearchResult ) -> String? {
var returnString = ""
if let unwrappedString = forSearchResult.capturedStrings.first {
returnString = unwrappedString
} else {
return nil
}
var numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
numberFormatter.maximumFractionDigits = ColorBuilder.maximumFractionDigits
numberFormatter.minimumFractionDigits = 1
if ( forSearchResult.tcr.numberOfRanges == 5 ) {
if let modifiedString = processCaptureGroupInSearchResult( forSearchResult, forRangeAtIndex: 4, inText: returnString, withReplacementText: "\(color.alphaComponent)" ) {
returnString = modifiedString
} else {
return nil
}
} else if ( color.alphaComponent < 1.0 ) {
// User has changed the alpha so we need to add it to the code.
if let modifiedString = processCaptureGroupInSearchResult( forSearchResult, forRangeAtIndex: 3, inText: returnString, withReplacementText: forSearchResult.capturedStrings[3] + ", alpha: \(color.alphaComponent)" ) {
returnString = modifiedString
} else {
return nil
}
}
if let component = numberFormatter.stringFromNumber( Double ( color.blueComponent ) ) {
if let modifiedString = processCaptureGroupInSearchResult( forSearchResult, forRangeAtIndex: 3, inText: returnString, withReplacementText: component ) {
returnString = modifiedString
} else {
return nil
}
} else {
println ( "Error converting blue component with value of \(color.blueComponent) to a string." )
return nil
}
if let component = numberFormatter.stringFromNumber( Double ( color.greenComponent ) ) {
if let modifiedString = processCaptureGroupInSearchResult( forSearchResult, forRangeAtIndex: 2, inText: returnString, withReplacementText: component ) {
returnString = modifiedString
} else {
return nil
}
} else {
println ( "Error converting green component with value of \(color.greenComponent) to a string." )
return nil
}
if let component = numberFormatter.stringFromNumber( Double ( color.redComponent ) ) {
if let modifiedString = processCaptureGroupInSearchResult( forSearchResult, forRangeAtIndex: 1, inText: returnString, withReplacementText: component ) {
returnString = modifiedString
} else {
return nil
}
} else {
println ( "Error converting red component with value of \(color.redComponent) to a string." )
return nil
}
return returnString
}
}