@@ -230,6 +230,43 @@ extension String {
230230 guard let converter = ColorApproximation ( color: color) else { return self }
231231 return applyingColor ( converter. convert ( to: target) )
232232 }
233+
234+ /// String with an HSL color applied to the text. The exact color which will be used is determined by the `target`.
235+ ///
236+ /// - Parameters:
237+ /// - hue: The hue component in degrees (0-360). Values outside this range will be wrapped around.
238+ /// - saturation: The saturation component as a percentage (0-100). Values outside will be clamped.
239+ /// - lightness: The lightness component as a percentage (0-100). Values outside will be clamped.
240+ /// - target: The conversion target of this color. If `target` is `.bit8Approximated`, an approximated 8-bit
241+ /// color will be used; If `.bit24`, a 24-bit color is used.
242+ /// Default is `.bit8Approximated`.
243+ /// - Returns: The formatted string with the HSL color applied.
244+ public func hsl( _ hue: Double , _ saturation: Double , _ lightness: Double , to target: HexColorTarget = . bit8Approximated) -> String {
245+ let converter = HSLColorConverter ( hue: hue, saturation: saturation, lightness: lightness)
246+ let rgb = converter. toRGB ( )
247+
248+ switch target {
249+ case . bit8Approximated:
250+ // Convert RGB to hex and use ColorApproximation for 8-bit conversion
251+ let hexValue = ( UInt32 ( rgb. 0 ) << 16 ) | ( UInt32 ( rgb. 1 ) << 8 ) | UInt32 ( rgb. 2 )
252+ guard let approximator = ColorApproximation ( color: hexValue) else { return self }
253+ return applyingColor ( approximator. convert ( to: . bit8Approximated) )
254+ case . bit24:
255+ return applyingColor ( . bit24( rgb) )
256+ }
257+ }
258+
259+ /// String with an HSL color applied to the text. The exact color which will be used is determined by the `target`.
260+ ///
261+ /// - Parameters:
262+ /// - hsl: The HSL color as a tuple (hue: 0-360, saturation: 0-100, lightness: 0-100).
263+ /// - target: The conversion target of this color. If `target` is `.bit8Approximated`, an approximated 8-bit
264+ /// color will be used; If `.bit24`, a 24-bit color is used.
265+ /// Default is `.bit8Approximated`.
266+ /// - Returns: The formatted string with the HSL color applied.
267+ public func hsl( _ hsl: HSL , to target: HexColorTarget = . bit8Approximated) -> String {
268+ return self . hsl ( hsl. hue, hsl. saturation, hsl. lightness, to: target)
269+ }
233270}
234271
235272// MARK: - Background Colors Shorthand
@@ -304,6 +341,43 @@ extension String {
304341 guard let converter = ColorApproximation ( color: color) else { return self }
305342 return applyingBackgroundColor ( converter. convert ( to: target) )
306343 }
344+
345+ /// String with an HSL color applied to the background. The exact color which will be used is determined by the `target`.
346+ ///
347+ /// - Parameters:
348+ /// - hue: The hue component in degrees (0-360). Values outside this range will be wrapped around.
349+ /// - saturation: The saturation component as a percentage (0-100). Values outside will be clamped.
350+ /// - lightness: The lightness component as a percentage (0-100). Values outside will be clamped.
351+ /// - target: The conversion target of this color. If `target` is `.bit8Approximated`, an approximated 8-bit
352+ /// color will be used; If `.bit24`, a 24-bit color is used.
353+ /// Default is `.bit8Approximated`.
354+ /// - Returns: The formatted string with the HSL color applied to the background.
355+ public func onHsl( _ hue: Double , _ saturation: Double , _ lightness: Double , to target: HexColorTarget = . bit8Approximated) -> String {
356+ let converter = HSLColorConverter ( hue: hue, saturation: saturation, lightness: lightness)
357+ let rgb = converter. toRGB ( )
358+
359+ switch target {
360+ case . bit8Approximated:
361+ // Convert RGB to hex and use ColorApproximation for 8-bit conversion
362+ let hexValue = ( UInt32 ( rgb. 0 ) << 16 ) | ( UInt32 ( rgb. 1 ) << 8 ) | UInt32 ( rgb. 2 )
363+ guard let approximator = ColorApproximation ( color: hexValue) else { return self }
364+ return applyingBackgroundColor ( approximator. convert ( to: target) )
365+ case . bit24:
366+ return applyingBackgroundColor ( . bit24( rgb) )
367+ }
368+ }
369+
370+ /// String with an HSL color applied to the background. The exact color which will be used is determined by the `target`.
371+ ///
372+ /// - Parameters:
373+ /// - hsl: The HSL color as a tuple (hue: 0-360, saturation: 0-100, lightness: 0-100).
374+ /// - target: The conversion target of this color. If `target` is `.bit8Approximated`, an approximated 8-bit
375+ /// color will be used; If `.bit24`, a 24-bit color is used.
376+ /// Default is `.bit8Approximated`.
377+ /// - Returns: The formatted string with the HSL color applied to the background.
378+ public func onHsl( _ hsl: HSL , to target: HexColorTarget = . bit8Approximated) -> String {
379+ return self . onHsl ( hsl. hue, hsl. saturation, hsl. lightness, to: target)
380+ }
307381}
308382
309383// MARK: - Styles Shorthand
0 commit comments