diff --git a/Example/Example/Base.lproj/Main.storyboard b/Example/Example/Base.lproj/Main.storyboard index f4a2b79..bbbc792 100644 --- a/Example/Example/Base.lproj/Main.storyboard +++ b/Example/Example/Base.lproj/Main.storyboard @@ -43,7 +43,7 @@ - + diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index 1831243..61d7aa3 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -30,17 +30,12 @@ class ViewController: UIViewController { @IBOutlet weak var messageLabel: UILabel! var coder = RCCoder() var image = RCImage(message: "") - let colors: [[UIColor]] = [[.orange, .gray], [.orange, .magenta], [.systemGreen, .systemBlue], [.orange, .brown], [.purple, .cyan]] + let colors: [[UIColor]] = [[.orange, .magenta], [.systemPink, .systemTeal], [.systemTeal, .systemGreen], [.orange, .brown], [.purple, .cyan]] var isScanningFromLibrary = false } extension ViewController { - override func viewDidLoad() { - super.viewDidLoad() - image.isTransparent = true - } - @IBAction func scanImage(_ sender: Any) { isScanningFromLibrary = true let vc = UIImagePickerController() diff --git a/RoundCode.podspec b/RoundCode.podspec index ad2b283..ec8a173 100644 --- a/RoundCode.podspec +++ b/RoundCode.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'RoundCode' - s.version = '1.1.0' + s.version = '1.1.1' s.summary = 'Facebook messenger style custom barcode.' s.description = <<-DESC Encode and decode data into custom stylish barcode. diff --git a/Sources/Internal/RCConstants.swift b/Sources/Internal/RCConstants.swift index c8a9010..7706d2a 100644 --- a/Sources/Internal/RCConstants.swift +++ b/Sources/Internal/RCConstants.swift @@ -28,5 +28,5 @@ struct RCConstants { static let dotSizeScale: CGFloat = 0.08 static let dotPatterns: [CGFloat] = [6, 4, 2] static let dotPointRange = (Float(1.3)...Float(2.5)) - static let pixelThreshold = 180 + static let pixelThreshold = (70...180) } diff --git a/Sources/Internal/RCError.swift b/Sources/Internal/RCError.swift index 944a6a0..5855777 100644 --- a/Sources/Internal/RCError.swift +++ b/Sources/Internal/RCError.swift @@ -27,6 +27,7 @@ public enum RCError: String, LocalizedError { case longText case decoding case wrongImageSize + case wrongColors public var errorDescription: String? { switch self { @@ -38,6 +39,8 @@ public enum RCError: String, LocalizedError { return "Error decoding" case .wrongImageSize: return "Error decoding. Image width and height must be a equal" + case .wrongColors: + return "The tint colors should be in a range of \(RCConstants.pixelThreshold) grayscale with 1.0 alpha" } } } diff --git a/Sources/Internal/RCImageDecoder.swift b/Sources/Internal/RCImageDecoder.swift index a8f46cd..5a743f7 100644 --- a/Sources/Internal/RCImageDecoder.swift +++ b/Sources/Internal/RCImageDecoder.swift @@ -57,7 +57,7 @@ extension RCImageDecoder { let transform = calculateTransform(from: points) let mapper = RCPointMapper(transform: transform, size: size) let locations = mapper.map(points: calculateBitLocations()) - let bits = locations.map { data[Int($0.x), Int($0.y)] > RCConstants.pixelThreshold ? RCBit.zero : RCBit.one } + let bits = locations.map { RCConstants.pixelThreshold.contains(Int(data[Int($0.x), Int($0.y)])) ? RCBit.one : RCBit.zero } return bits } @@ -75,13 +75,13 @@ extension RCImageDecoder { private func scanControlPoint(for data: PixelContainer, region: (x: Int, y: Int), side: Side) throws -> CGPoint { func scan(region: (x: Int, y: Int, size: Int), data: PixelContainer, coordinate: (Int) -> (x: Int, y: Int), comparison: (PixelPattern, (x: Int, y: Int)) -> Bool) -> [PixelPattern] { - var lastPattern = PixelPattern(bit: data[region.x, region.y] > RCConstants.pixelThreshold ? RCBit.zero : RCBit.one, x: region.x, y: region.y, count: 0) + var lastPattern = PixelPattern(bit: RCConstants.pixelThreshold.contains(Int(data[region.x, region.y])) ? RCBit.one : RCBit.zero, x: region.x, y: region.y, count: 0) var pixelPatterns = [lastPattern] var count = 0 let maxSize = region.size * region.size while count < maxSize { let coordinate = coordinate(count) - let bit = data[coordinate.x, coordinate.y] > RCConstants.pixelThreshold ? RCBit.zero : RCBit.one + let bit = RCConstants.pixelThreshold.contains(Int(data[coordinate.x, coordinate.y])) ? RCBit.one : RCBit.zero if comparison(lastPattern, coordinate), lastPattern.bit == bit { lastPattern.count += 1 pixelPatterns[pixelPatterns.count - 1] = lastPattern diff --git a/Sources/Public/RCCoder.swift b/Sources/Public/RCCoder.swift index 25d0c79..6579d3f 100644 --- a/Sources/Public/RCCoder.swift +++ b/Sources/Public/RCCoder.swift @@ -54,6 +54,16 @@ public extension RCCoder { func validate(_ text: String) -> Bool { configuration.validate(text) } + + func validateForBlackBackground(colors: [UIColor]) -> Bool { + colors.allSatisfy { color in + var white: CGFloat = 0 + var alpha: CGFloat = 0 + guard color.getWhite(&white, alpha: &alpha) else { return false } + guard alpha == 1.0 else { return false } + return RCConstants.pixelThreshold.contains(Int(white * 255)) + } + } } extension RCCoder { diff --git a/Sources/Public/RCImage.swift b/Sources/Public/RCImage.swift index 5dec568..8cb4f08 100644 --- a/Sources/Public/RCImage.swift +++ b/Sources/Public/RCImage.swift @@ -28,7 +28,7 @@ public struct RCImage { public var size = CGFloat(300) public var tintColors: [UIColor] = [.orange, .magenta] public var attachmentImage: UIImage? - public var isTransparent = false + public var isTransparent = true public var gradientType = GradientType.linear(angle: CGFloat.pi / 4) public var contentInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)