-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #51.
- Loading branch information
Showing
11 changed files
with
143 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/// A representation of a color. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Color { | ||
public static let black = Color(.black) | ||
public static let white = Color(.white) | ||
|
||
/// Creates a Color instance using a Tailwind CSS palette instance. | ||
public init(_ colorPalette: ColorPalette, darkness: Int) { | ||
self.storage = .palette(colorPalette, darkness: darkness) | ||
} | ||
|
||
init(_ value: Storage) { | ||
self.storage = value | ||
} | ||
|
||
enum Storage { | ||
case black | ||
case white | ||
case palette(ColorPalette, darkness: Int) | ||
} | ||
private let storage: Storage | ||
|
||
/// Returns the Tailwind CSS color class for this color. | ||
func toTailwindColorClass() -> String { | ||
switch storage { | ||
case .black: | ||
return "black" | ||
case .white: | ||
return "white" | ||
case .palette(let colorPalette, let darkness): | ||
return colorPalette.closestTailwindColorStop(for: darkness) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/// A representation of a Tailwind CSS color palette. | ||
/// | ||
/// - SeeAlso: Tailwind CSS' [`color palettes`](https://tailwindcss.com/docs/customizing-colors) documentation. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public enum ColorPalette: String { | ||
|
||
// MARK: - Grays | ||
|
||
case slate | ||
case gray | ||
case zinc | ||
case neutral | ||
case stone | ||
|
||
// MARK: - Tones, in order of the rainbow | ||
|
||
case red | ||
case orange | ||
case amber | ||
case yellow | ||
case lime | ||
case green | ||
case emerald | ||
case teal | ||
case cyan | ||
case sky | ||
case blue | ||
case indigo | ||
case violet | ||
case purple | ||
case fuchsia | ||
case pink | ||
case rose | ||
|
||
/// Chooses the closest tailwind color stop for the given darkness value. | ||
func closestTailwindColorStop(for darkness: Int) -> String { | ||
// Define the range of Tailwind CSS color palette numbers | ||
let paletteNumbers = [0, 50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950, 1000] | ||
|
||
// Find the closest palette number | ||
guard let closest = paletteNumbers.min(by: { abs($0 - darkness) < abs($1 - darkness) }) else { | ||
return "500" // Default to middle value if no closest found | ||
} | ||
|
||
if closest == 0 { | ||
return "white" | ||
} else if closest == 1000 { | ||
return "black" | ||
} | ||
return "\(rawValue)-\(closest)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Sources/Slipstream/TailwindCSS/Typography/View+textColor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extension View { | ||
/// Sets the text color to a specific Tailwind CSS palette. | ||
/// | ||
/// - Parameters: | ||
/// - colorPalette: The Tailwind CSS color palette to use. | ||
/// - darkness: The darkness bracket within the color palette to use. | ||
/// Higher values are darker. 0 corresponds to white, 1000 to black. | ||
/// | ||
/// - SeeAlso: Tailwind CSS' [`text-color`](https://tailwindcss.com/docs/text-color) documentation. | ||
/// - SeeAlso: Tailwind CSS' [customizing colors](https://tailwindcss.com/docs/customizing-colors) article. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public func textColor(_ colorPalette: ColorPalette, darkness: Int) -> some View { | ||
return modifier(ClassModifier(add: "text-" + colorPalette.closestTailwindColorStop(for: darkness))) | ||
} | ||
|
||
/// Sets the text color. | ||
/// | ||
/// - SeeAlso: Tailwind CSS' [`text-color`](https://tailwindcss.com/docs/text-color) documentation. | ||
/// - SeeAlso: Tailwind CSS' [customizing colors](https://tailwindcss.com/docs/customizing-colors) article. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public func textColor(_ color: Color) -> some View { | ||
return modifier(ClassModifier(add: "text-" + color.toTailwindColorClass())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Testing | ||
import Slipstream | ||
|
||
struct TextColorTests { | ||
@Test func darknessValues() throws { | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 0)) == #"<div class="text-white"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 50)) == #"<div class="text-red-50"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 100)) == #"<div class="text-red-100"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 200)) == #"<div class="text-red-200"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 300)) == #"<div class="text-red-300"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 400)) == #"<div class="text-red-400"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 500)) == #"<div class="text-red-500"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 600)) == #"<div class="text-red-600"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 700)) == #"<div class="text-red-700"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 800)) == #"<div class="text-red-800"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 900)) == #"<div class="text-red-900"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 950)) == #"<div class="text-red-950"></div>"#) | ||
try #expect(renderHTML(Div {}.textColor(.red, darkness: 1000)) == #"<div class="text-black"></div>"#) | ||
} | ||
} |