Skip to content

Commit 073d790

Browse files
committed
Add colorSpace parameter to lighten and darken
1 parent 09c8194 commit 073d790

File tree

1 file changed

+84
-44
lines changed

1 file changed

+84
-44
lines changed

Sources/ReerKit/Shared/Color+REExtensions.swift

Lines changed: 84 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -346,35 +346,47 @@ public extension Reer where Base: REColor {
346346
/// let color = UIColor(red: r, green: g, blue: b, alpha: a)
347347
/// let lighterColor: UIColor = color.lighten(by: 0.2)
348348
///
349-
/// - Parameter percentage: Percentage by which to lighten the color
349+
/// - Parameters:
350+
/// - percentage: Percentage by which to lighten the color
351+
/// - colorSpace: color space (default is sRGB)
350352
/// - Returns: A lightened color
351-
func lighten(by percentage: CGFloat = 0.2) -> REColor {
353+
func lighten(by percentage: CGFloat = 0.2, colorSpace: ColorSpace = .sRGB) -> REColor {
352354
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
353355
base.getRed(&r, green: &g, blue: &b, alpha: &a)
354-
return REColor(
355-
red: r + (1 - r) * percentage,
356-
green: g + (1 - g) * percentage,
357-
blue: b + (1 - b) * percentage,
358-
alpha: a
359-
)
356+
let newR = r + (1 - r) * percentage
357+
let newG = g + (1 - g) * percentage
358+
let newB = b + (1 - b) * percentage
359+
360+
switch colorSpace {
361+
case .sRGB:
362+
return REColor(red: newR, green: newG, blue: newB, alpha: a)
363+
case .displayP3:
364+
return REColor(displayP3Red: newR, green: newG, blue: newB, alpha: a)
365+
}
360366
}
361367

362368
/// ReerKit: Darken a color
363369
///
364370
/// let color = UIColor(red: r, green: g, blue: b, alpha: a)
365371
/// let darkerColor: UIColor = color.darken(by: 0.2)
366372
///
367-
/// - Parameter percentage: Percentage by which to darken the color
373+
/// - Parameters:
374+
/// - percentage: Percentage by which to darken the color
375+
/// - colorSpace: color space (default is sRGB)
368376
/// - Returns: A darkened color
369-
func darken(by percentage: CGFloat = 0.2) -> REColor {
377+
func darken(by percentage: CGFloat = 0.2, colorSpace: ColorSpace = .sRGB) -> REColor {
370378
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
371379
base.getRed(&r, green: &g, blue: &b, alpha: &a)
372-
return REColor(
373-
red: r - r * percentage,
374-
green: g - g * percentage,
375-
blue: b - b * percentage,
376-
alpha: a
377-
)
380+
let newR = r - r * percentage
381+
let newG = g - g * percentage
382+
let newB = b - b * percentage
383+
384+
switch colorSpace {
385+
case .sRGB:
386+
return REColor(red: newR, green: newG, blue: newB, alpha: a)
387+
case .displayP3:
388+
return REColor(displayP3Red: newR, green: newG, blue: newB, alpha: a)
389+
}
378390
}
379391

380392
/// ReerKit: Blend two Colors.
@@ -384,12 +396,14 @@ public extension Reer where Base: REColor {
384396
/// - intensity1: intensity of first color (default is 0.5)
385397
/// - color2: second color to blend
386398
/// - intensity2: intensity of second color (default is 0.5)
399+
/// - colorSpace: color space (default is sRGB)
387400
/// - Returns: Color created by blending first and second colors.
388401
static func blend(
389402
_ color1: REColor,
390403
intensity1: CGFloat = 0.5,
391404
with color2: REColor,
392-
intensity2: CGFloat = 0.5
405+
intensity2: CGFloat = 0.5,
406+
colorSpace: ColorSpace = .sRGB
393407
) -> REColor {
394408
let total = intensity1 + intensity2
395409
let level1 = intensity1 / total
@@ -418,15 +432,22 @@ public extension Reer where Base: REColor {
418432
let blue = level1 * blue1 + level2 * blue2
419433
let alpha = level1 * alpha1 + level2 * alpha2
420434

421-
return REColor(red: red, green: green, blue: blue, alpha: alpha)
435+
switch colorSpace {
436+
case .sRGB:
437+
return REColor(red: red, green: green, blue: blue, alpha: alpha)
438+
case .displayP3:
439+
return REColor(displayP3Red: red, green: green, blue: blue, alpha: alpha)
440+
}
422441
}
423442

424443
/// ReerKit: Blend the color with a color
425444
///
426-
/// - Parameter color: second color to blend
445+
/// - Parameters:
446+
/// - color: second color to blend
447+
/// - colorSpace: color space (default is sRGB)
427448
/// - Returns: Color created by blending self and seond colors.
428-
func blend(with color: REColor) -> REColor {
429-
return REColor.re.blend(base, intensity1: 0.5, with: color, intensity2: 0.5)
449+
func blend(with color: REColor, colorSpace: ColorSpace = .sRGB) -> REColor {
450+
return REColor.re.blend(base, intensity1: 0.5, with: color, intensity2: 0.5, colorSpace: colorSpace)
430451
}
431452
}
432453

@@ -769,45 +790,55 @@ public extension ReerForEquatable where Base == Color {
769790
/// let color = Color.re(hex: 0xFF8040)
770791
/// let lighterColor = color.re.lighten(by: 0.2)
771792
///
772-
/// - Parameter percentage: Percentage by which to lighten the color
793+
/// - Parameters:
794+
/// - percentage: Percentage by which to lighten the color
795+
/// - colorSpace: color space (default is sRGB)
773796
/// - Returns: A lightened SwiftUI Color
774-
func lighten(by percentage: CGFloat = 0.2) -> Color {
797+
func lighten(by percentage: CGFloat = 0.2, colorSpace: ColorSpace = .sRGB) -> Color {
775798
let comps = rgbaPercent
776799
let r = comps.red
777800
let g = comps.green
778801
let b = comps.blue
779802
let a = comps.alpha
780803

781-
return Color(
782-
.sRGB,
783-
red: r + (1 - r) * percentage,
784-
green: g + (1 - g) * percentage,
785-
blue: b + (1 - b) * percentage,
786-
opacity: Double(a)
787-
)
804+
let newR = r + (1 - r) * percentage
805+
let newG = g + (1 - g) * percentage
806+
let newB = b + (1 - b) * percentage
807+
808+
switch colorSpace {
809+
case .sRGB:
810+
return Color(.sRGB, red: newR, green: newG, blue: newB, opacity: Double(a))
811+
case .displayP3:
812+
return Color(.displayP3, red: newR, green: newG, blue: newB, opacity: Double(a))
813+
}
788814
}
789815

790816
/// ReerKit: Darken a color
791817
///
792818
/// let color = Color.re(hex: 0xFF8040)
793819
/// let darkerColor = color.re.darken(by: 0.2)
794820
///
795-
/// - Parameter percentage: Percentage by which to darken the color
821+
/// - Parameters:
822+
/// - percentage: Percentage by which to darken the color
823+
/// - colorSpace: color space (default is sRGB)
796824
/// - Returns: A darkened SwiftUI Color
797-
func darken(by percentage: CGFloat = 0.2) -> Color {
825+
func darken(by percentage: CGFloat = 0.2, colorSpace: ColorSpace = .sRGB) -> Color {
798826
let comps = rgbaPercent
799827
let r = comps.red
800828
let g = comps.green
801829
let b = comps.blue
802830
let a = comps.alpha
803831

804-
return Color(
805-
.sRGB,
806-
red: r - r * percentage,
807-
green: g - g * percentage,
808-
blue: b - b * percentage,
809-
opacity: Double(a)
810-
)
832+
let newR = r - r * percentage
833+
let newG = g - g * percentage
834+
let newB = b - b * percentage
835+
836+
switch colorSpace {
837+
case .sRGB:
838+
return Color(.sRGB, red: newR, green: newG, blue: newB, opacity: Double(a))
839+
case .displayP3:
840+
return Color(.displayP3, red: newR, green: newG, blue: newB, opacity: Double(a))
841+
}
811842
}
812843

813844
/// ReerKit: Blend two SwiftUI Colors.
@@ -817,12 +848,14 @@ public extension ReerForEquatable where Base == Color {
817848
/// - intensity1: intensity of first color (default is 0.5)
818849
/// - color2: second color to blend
819850
/// - intensity2: intensity of second color (default is 0.5)
851+
/// - colorSpace: color space (default is sRGB)
820852
/// - Returns: Color created by blending first and second colors.
821853
static func blend(
822854
_ color1: Color,
823855
intensity1: CGFloat = 0.5,
824856
with color2: Color,
825-
intensity2: CGFloat = 0.5
857+
intensity2: CGFloat = 0.5,
858+
colorSpace: ColorSpace = .sRGB
826859
) -> Color {
827860
let total = intensity1 + intensity2
828861
let level1 = intensity1 / total
@@ -851,15 +884,22 @@ public extension ReerForEquatable where Base == Color {
851884
let blue = level1 * blue1 + level2 * blue2
852885
let alpha = level1 * alpha1 + level2 * alpha2
853886

854-
return Color(.sRGB, red: Double(red), green: Double(green), blue: Double(blue), opacity: Double(alpha))
887+
switch colorSpace {
888+
case .sRGB:
889+
return Color(.sRGB, red: Double(red), green: Double(green), blue: Double(blue), opacity: Double(alpha))
890+
case .displayP3:
891+
return Color(.displayP3, red: Double(red), green: Double(green), blue: Double(blue), opacity: Double(alpha))
892+
}
855893
}
856894

857895
/// ReerKit: Blend the color with another SwiftUI color
858896
///
859-
/// - Parameter color: second color to blend
897+
/// - Parameters:
898+
/// - color: second color to blend
899+
/// - colorSpace: color space (default is sRGB)
860900
/// - Returns: Color created by blending self and second colors.
861-
func blend(with color: Color) -> Color {
862-
return Color.re.blend(base, intensity1: 0.5, with: color, intensity2: 0.5)
901+
func blend(with color: Color, colorSpace: ColorSpace = .sRGB) -> Color {
902+
return Color.re.blend(base, intensity1: 0.5, with: color, intensity2: 0.5, colorSpace: colorSpace)
863903
}
864904

865905
private var rgbaComponents: [CGFloat] {

0 commit comments

Comments
 (0)