Skip to content

Commit d3aaa14

Browse files
committed
docs: Add comprehensive markdown documentation for color conversion utilities
1 parent fcd3f60 commit d3aaa14

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

docs/Color+Conversion.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Color Conversion Utilities
2+
3+
SwiftDevKit provides a comprehensive set of color conversion utilities that work seamlessly across iOS and macOS platforms. These utilities enable easy conversion between different color spaces and formats commonly used in digital design and development.
4+
5+
## Supported Color Spaces
6+
7+
- RGB (Red, Green, Blue)
8+
- HSL (Hue, Saturation, Lightness)
9+
- HSV (Hue, Saturation, Value)
10+
- CMYK (Cyan, Magenta, Yellow, Key/Black)
11+
- Hex string representation
12+
13+
## Value Ranges
14+
15+
All color components are normalized to use consistent value ranges:
16+
17+
- RGB components (red, green, blue): 0 to 1
18+
- HSL/HSV hue: 0 to 360 degrees
19+
- HSL/HSV saturation: 0 to 1
20+
- HSL lightness: 0 to 1
21+
- HSV value: 0 to 1
22+
- CMYK components: 0 to 1
23+
- Alpha: 0 to 1
24+
25+
## Usage Examples
26+
27+
### Creating Colors
28+
29+
```swift
30+
// From hex string
31+
let redFromHex = Color(hex: "#FF0000")
32+
let blueFromHex = Color(hex: "0000FF") // # is optional
33+
34+
// From HSL
35+
let greenFromHSL = Color(hsl: HSL(
36+
hue: 120, // Green hue (120 degrees)
37+
saturation: 1.0, // Full saturation
38+
lightness: 0.5 // Medium lightness
39+
))
40+
41+
// From HSV
42+
let blueFromHSV = Color(hsv: HSV(
43+
hue: 240, // Blue hue (240 degrees)
44+
saturation: 1.0, // Full saturation
45+
value: 1.0 // Maximum brightness
46+
))
47+
48+
// From CMYK
49+
let yellowFromCMYK = Color(cmyk: CMYK(
50+
cyan: 0, // No cyan
51+
magenta: 0, // No magenta
52+
yellow: 1.0, // Full yellow
53+
key: 0 // No black
54+
))
55+
```
56+
57+
### Converting Colors
58+
59+
```swift
60+
let color = Color.red
61+
62+
// To hex string
63+
let hex = color.toHex() // "#FF0000"
64+
let hexNoHash = color.toHex(includeHash: false) // "FF0000"
65+
66+
// To HSL
67+
let hsl = color.toHSL()
68+
// HSL(hue: 0, saturation: 1.0, lightness: 0.5)
69+
70+
// To HSV
71+
let hsv = color.toHSV()
72+
// HSV(hue: 0, saturation: 1.0, value: 1.0)
73+
74+
// To CMYK
75+
let cmyk = color.toCMYK()
76+
// CMYK(cyan: 0, magenta: 1.0, yellow: 1.0, key: 0)
77+
```
78+
79+
## Color Models
80+
81+
### RGB Structure
82+
```swift
83+
public struct RGB {
84+
public let red: CGFloat // 0-1
85+
public let green: CGFloat // 0-1
86+
public let blue: CGFloat // 0-1
87+
public let alpha: CGFloat // 0-1
88+
}
89+
```
90+
91+
### HSL Structure
92+
```swift
93+
public struct HSL {
94+
public let hue: CGFloat // 0-360
95+
public let saturation: CGFloat // 0-1
96+
public let lightness: CGFloat // 0-1
97+
}
98+
```
99+
100+
### HSV Structure
101+
```swift
102+
public struct HSV {
103+
public let hue: CGFloat // 0-360
104+
public let saturation: CGFloat // 0-1
105+
public let value: CGFloat // 0-1
106+
}
107+
```
108+
109+
### CMYK Structure
110+
```swift
111+
public struct CMYK {
112+
public let cyan: CGFloat // 0-1
113+
public let magenta: CGFloat // 0-1
114+
public let yellow: CGFloat // 0-1
115+
public let key: CGFloat // 0-1
116+
}
117+
```
118+
119+
## Platform Compatibility
120+
121+
The color conversion utilities work consistently across both iOS and macOS platforms:
122+
123+
- iOS: Uses `UIColor` as the base color type
124+
- macOS: Uses `NSColor` as the base color type
125+
126+
The code automatically handles the appropriate color type through a type alias:
127+
128+
```swift
129+
#if canImport(UIKit)
130+
public typealias Color = UIColor
131+
#elseif canImport(AppKit)
132+
public typealias Color = NSColor
133+
#endif
134+
```
135+
136+
## Error Handling
137+
138+
- Hex string initialization returns an optional and fails gracefully if the string format is invalid
139+
- Color space conversions handle edge cases and provide reasonable defaults
140+
- Platform-specific color space conversions are handled automatically
141+
142+
## Best Practices
143+
144+
1. Always check if hex string initialization succeeded before using the color
145+
2. Use the appropriate color space for your specific needs:
146+
- HSL: When you need intuitive control over lightness
147+
- HSV: When you need intuitive control over brightness/value
148+
- CMYK: When working with print-related colors
149+
- RGB/Hex: When working with web colors or digital displays
150+
3. Consider color space conversion implications when targeting both iOS and macOS

0 commit comments

Comments
 (0)