فهرست منبع

Merge pull request #1826 from onevcat/fix/color-space-hex

Use color description instead of color hex for processor Id
Wei Wang 4 سال پیش
والد
کامیت
ef39a51f16
2فایلهای تغییر یافته به همراه36 افزوده شده و 6 حذف شده
  1. 11 5
      Sources/Image/ImageProcessor.swift
  2. 25 1
      Tests/KingfisherTests/ImageProcessorTests.swift

+ 11 - 5
Sources/Image/ImageProcessor.swift

@@ -216,7 +216,7 @@ public struct BlendImageProcessor: ImageProcessor {
         self.backgroundColor = backgroundColor
         self.backgroundColor = backgroundColor
         var identifier = "com.onevcat.Kingfisher.BlendImageProcessor(\(blendMode.rawValue),\(alpha))"
         var identifier = "com.onevcat.Kingfisher.BlendImageProcessor(\(blendMode.rawValue),\(alpha))"
         if let color = backgroundColor {
         if let color = backgroundColor {
-            identifier.append("_\(color.hex)")
+            identifier.append("_\(color.rgbaDescription)")
         }
         }
         self.identifier = identifier
         self.identifier = identifier
     }
     }
@@ -489,7 +489,7 @@ public struct Border {
     }
     }
     
     
     var identifier: String {
     var identifier: String {
-        "\(color.hex)_\(lineWidth)_\(radius.radiusIdentifier)_\(roundingCorners.cornerIdentifier)"
+        "\(color.rgbaDescription)_\(lineWidth)_\(radius.radiusIdentifier)_\(roundingCorners.cornerIdentifier)"
     }
     }
 }
 }
 
 
@@ -650,7 +650,7 @@ public struct OverlayImageProcessor: ImageProcessor {
     public init(overlay: KFCrossPlatformColor, fraction: CGFloat = 0.5) {
     public init(overlay: KFCrossPlatformColor, fraction: CGFloat = 0.5) {
         self.overlay = overlay
         self.overlay = overlay
         self.fraction = fraction
         self.fraction = fraction
-        self.identifier = "com.onevcat.Kingfisher.OverlayImageProcessor(\(overlay.hex)_\(fraction))"
+        self.identifier = "com.onevcat.Kingfisher.OverlayImageProcessor(\(overlay.rgbaDescription)_\(fraction))"
     }
     }
     
     
     /// Processes the input `ImageProcessItem` with this processor.
     /// Processes the input `ImageProcessItem` with this processor.
@@ -687,7 +687,7 @@ public struct TintImageProcessor: ImageProcessor {
     /// - parameter tint: Tint color will be used to tint the input image.
     /// - parameter tint: Tint color will be used to tint the input image.
     public init(tint: KFCrossPlatformColor) {
     public init(tint: KFCrossPlatformColor) {
         self.tint = tint
         self.tint = tint
-        self.identifier = "com.onevcat.Kingfisher.TintImageProcessor(\(tint.hex))"
+        self.identifier = "com.onevcat.Kingfisher.TintImageProcessor(\(tint.rgbaDescription))"
     }
     }
     
     
     /// Processes the input `ImageProcessItem` with this processor.
     /// Processes the input `ImageProcessItem` with this processor.
@@ -908,7 +908,7 @@ extension KFCrossPlatformColor {
         var a: CGFloat = 0
         var a: CGFloat = 0
 
 
         #if os(macOS)
         #if os(macOS)
-        (usingColorSpace(.sRGB) ?? self).getRed(&r, green: &g, blue: &b, alpha: &a)
+        (usingColorSpace(.extendedSRGB) ?? self).getRed(&r, green: &g, blue: &b, alpha: &a)
         #else
         #else
         getRed(&r, green: &g, blue: &b, alpha: &a)
         getRed(&r, green: &g, blue: &b, alpha: &a)
         #endif
         #endif
@@ -916,6 +916,12 @@ extension KFCrossPlatformColor {
         return (r, g, b, a)
         return (r, g, b, a)
     }
     }
     
     
+    var rgbaDescription: String {
+        let components = self.rgba
+        return String(format: "(%.2f,%.2f,%.2f,%.2f)", components.r, components.g, components.b, components.a)
+    }
+    
+    @available(*, deprecated, message: "`hex` is not safe for colors in extended space. Do not use this.")
     var hex: String {
     var hex: String {
         
         
         let (r, g, b, a) = rgba
         let (r, g, b, a) = rgba

+ 25 - 1
Tests/KingfisherTests/ImageProcessorTests.swift

@@ -25,7 +25,7 @@
 //  THE SOFTWARE.
 //  THE SOFTWARE.
 
 
 import XCTest
 import XCTest
-import Kingfisher
+@testable import Kingfisher
 
 
 class ImageProcessorTests: XCTestCase {
 class ImageProcessorTests: XCTestCase {
 
 
@@ -57,4 +57,28 @@ class ImageProcessorTests: XCTestCase {
         XCTAssertNotNil(two)
         XCTAssertNotNil(two)
         XCTAssertNotNil(three)
         XCTAssertNotNil(three)
     }
     }
+    
+    func testParsingColorRGBA() {
+        let sRGB = KFCrossPlatformColor(red: 0.5, green: 0.6, blue: 0.7, alpha: 0.8)
+        let rgba = sRGB.rgba
+        XCTAssertEqual(rgba.r, 0.5, accuracy: 0.01)
+        XCTAssertEqual(rgba.g, 0.6, accuracy: 0.01)
+        XCTAssertEqual(rgba.b, 0.7, accuracy: 0.01)
+        XCTAssertEqual(rgba.a, 0.8, accuracy: 0.01)
+        
+        let extended = KFCrossPlatformColor(displayP3Red: 0, green: 1, blue: 0, alpha: 0.8)
+        let rgbaExt = extended.rgba
+        // extended sRGB
+        XCTAssertTrue(rgbaExt.r < 0)
+        XCTAssertTrue(rgbaExt.g > 1)
+        XCTAssertTrue(rgbaExt.b < 0)
+        XCTAssertEqual(rgbaExt.a, 0.8)
+        
+        let blackWhite = KFCrossPlatformColor(white: 0.3, alpha: 1.0)
+        let rgbaBlackWhite = blackWhite.rgba
+        XCTAssertEqual(rgbaBlackWhite.r, 0.3, accuracy: 0.01)
+        XCTAssertEqual(rgbaBlackWhite.g, 0.3, accuracy: 0.01)
+        XCTAssertEqual(rgbaBlackWhite.b, 0.3, accuracy: 0.01)
+        XCTAssertEqual(rgbaBlackWhite.a, 1.0, accuracy: 0.01)
+    }
 }
 }