Skip to content

Commit

Permalink
fix: HSL issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
lilingxi01 committed Jan 16, 2023
1 parent 319a67e commit 5db90f3
Show file tree
Hide file tree
Showing 4 changed files with 1,406 additions and 1,372 deletions.
17 changes: 17 additions & 0 deletions generator/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// This part of code is referencing from: https://www.swiftbysundell.com/articles/defining-dynamic-colors-in-swift/
export const helperExtensions = `
// Algorithm coming from: https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV
extension Color {
/**
- Parameter h: Range from 0 to 360.
- Parameter s: Range from 0 to 1.
- Parameter l: Range from 0 to 1.
- Parameter a: Range from 0 to 1.
*/
init(h hue: Double, s saturation: Double, l lightness: Double, a opacity: Double = 1) {
let brightness = lightness + saturation * min(lightness, 1 - lightness)
let saturation = brightness == 0 ? 0 : 2 * (1 - lightness / brightness)
// Map to Color initializer.
self.init(hue: hue / 360, saturation: saturation, brightness: brightness, opacity: opacity)
}
}
#if canImport(UIKit)
extension UIColor {
convenience init(
Expand Down
4 changes: 2 additions & 2 deletions generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ function parseColorString(colorString: string): string {
const l = Number(parts[2]) / 100;
if (parts.length === 4) {
const a = Number(parts[3]);
return `Color(hue: ${h}, saturation: ${s.toFixed(3)}, brightness: ${l.toFixed(3)}, opacity: ${a.toFixed(3)})`;
return `Color(h: ${h}, s: ${s.toFixed(3)}, l: ${l.toFixed(3)}, a: ${a.toFixed(3)})`;
}
return `Color(hue: ${h}, saturation: ${s.toFixed(3)}, brightness: ${l.toFixed(3)})`;
return `Color(h: ${h}, s: ${s.toFixed(3)}, l: ${l.toFixed(3)})`;
}

const outputDir = 'output';
Expand Down
Loading

0 comments on commit 5db90f3

Please sign in to comment.