diff --git a/LensW2Demo/Extensions/Color+Hex.swift b/LensW2Demo/Extensions/Color+Hex.swift index 0d04320..83e10b3 100644 --- a/LensW2Demo/Extensions/Color+Hex.swift +++ b/LensW2Demo/Extensions/Color+Hex.swift @@ -9,34 +9,49 @@ import UIKit extension UIColor { class func color(from hex: String) -> UIColor? { - var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() - - if (cString.hasPrefix("#")) { - cString.remove(at: cString.startIndex) + var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines) + hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "") + + let scanner = Scanner(string: hexSanitized) + var rgb: UInt64 = 0 + + guard scanner.scanHexInt64(&rgb) else { + return nil } - - if ((cString.count) != 6) { + + var red: CGFloat = 0.0 + var green: CGFloat = 0.0 + var blue: CGFloat = 0.0 + var alpha: CGFloat = 1.0 + + if hexSanitized.count == 6 { + red = CGFloat((rgb >> 16) & 0xFF) / 255.0 + green = CGFloat((rgb >> 8) & 0xFF) / 255.0 + blue = CGFloat(rgb & 0xFF) / 255.0 + } else if hexSanitized.count == 8 { + red = CGFloat((rgb >> 24) & 0xFF) / 255.0 + green = CGFloat((rgb >> 16) & 0xFF) / 255.0 + blue = CGFloat((rgb >> 8) & 0xFF) / 255.0 + alpha = CGFloat(rgb & 0xFF) / 255.0 + } else { return nil } - - var rgbValue:UInt64 = 0 - Scanner(string: cString).scanHexInt64(&rgbValue) - - return UIColor( - red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, - green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, - blue: CGFloat(rgbValue & 0x0000FF) / 255.0, - alpha: CGFloat(1.0) - ) + + return UIColor(red: red, green: green, blue: blue, alpha: alpha) } func toHexString() -> String { - var r:CGFloat = 0 - var g:CGFloat = 0 - var b:CGFloat = 0 - var a:CGFloat = 1 - getRed(&r, green: &g, blue: &b, alpha: &a) - let rgb:Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0 - return String(format:"#%06x", rgb) + var red: CGFloat = 0.0 + var green: CGFloat = 0.0 + var blue: CGFloat = 0.0 + var alpha: CGFloat = 0.0 + + getRed(&red, green: &green, blue: &blue, alpha: &alpha) + + let hexRed = String(format: "%02X", Int(red * 255)) + let hexGreen = String(format: "%02X", Int(green * 255)) + let hexBlue = String(format: "%02X", Int(blue * 255)) + let hexAlpha = String(format: "%02X", Int(alpha * 255)) + return "#\(hexRed)\(hexGreen)\(hexBlue)\(hexAlpha)" } } diff --git a/LensW2Demo/LensSettingsViewController+ColorPickerDelegate.swift b/LensW2Demo/LensSettingsViewController+ColorPickerDelegate.swift index 59399d0..3fa7bdd 100644 --- a/LensW2Demo/LensSettingsViewController+ColorPickerDelegate.swift +++ b/LensW2Demo/LensSettingsViewController+ColorPickerDelegate.swift @@ -10,9 +10,7 @@ import UIKit extension LensSettingsViewController: UIColorPickerViewControllerDelegate { func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) { if let changingColor = changingColor, let indexPath = changingColorIndexPath { - if changingColor.1 == .colorCell { - jsonSettings[changingColor.0.rawValue] = viewController.selectedColor - } else if changingColor.1 == .stringColorCell { + if changingColor.1 == .stringColorCell { jsonSettings[changingColor.0.rawValue] = viewController.selectedColor.toHexString() } settingsTableView.reloadRows(at: [indexPath], with: .automatic) diff --git a/LensW2Demo/LensSettingsViewController+TableViewDataSource.swift b/LensW2Demo/LensSettingsViewController+TableViewDataSource.swift index 081a796..0a45b58 100644 --- a/LensW2Demo/LensSettingsViewController+TableViewDataSource.swift +++ b/LensW2Demo/LensSettingsViewController+TableViewDataSource.swift @@ -52,13 +52,6 @@ extension LensSettingsViewController: UITableViewDataSource { cell?.settingLabel.text = value cell?.selectionStyle = .none return cell ?? UITableViewCell() - case .colorCell: - let color = jsonSettings[title] as? UIColor - let cell = tableView.dequeueReusableCell(withIdentifier: "colorCell") as? ColorTableViewCell - cell?.titleLabel.text = title.titlecased() - cell?.settingView.backgroundColor = color ?? UIColor.green.withAlphaComponent(0.3) - cell?.selectionStyle = .none - return cell ?? UITableViewCell() case .stringColorCell: let rawColor = jsonSettings[title] as? String ?? "" let cell = tableView.dequeueReusableCell(withIdentifier: "colorCell") as? ColorTableViewCell diff --git a/LensW2Demo/LensSettingsViewController+TableViewDelegate.swift b/LensW2Demo/LensSettingsViewController+TableViewDelegate.swift index 0a6d972..8834301 100644 --- a/LensW2Demo/LensSettingsViewController+TableViewDelegate.swift +++ b/LensW2Demo/LensSettingsViewController+TableViewDelegate.swift @@ -12,7 +12,7 @@ extension LensSettingsViewController: UITableViewDelegate { let type = sections[indexPath.section][indexPath.row].1 if type == .stringValueCell || type == .doubleValueCell || type == .integerValueCell { handleValueCellTap(indexPath: indexPath) - } else if type == .colorCell || type == .stringColorCell { + } else if type == .stringColorCell { handleColorCellTap(indexPath: indexPath) } } diff --git a/LensW2Demo/LensSettingsViewController.swift b/LensW2Demo/LensSettingsViewController.swift index b6c655e..0c80e5e 100644 --- a/LensW2Demo/LensSettingsViewController.swift +++ b/LensW2Demo/LensSettingsViewController.swift @@ -21,7 +21,7 @@ class LensSettingsViewController: UIViewController { ] let generalSection: [(Keys,Types)] = [(.autoLightDetectionIsOn, .switchCell), (.stitchIsOn, .switchCell), (.allowSubmitUndetectedDocsIsOn, .switchCell), (.autoSubmitDocumentOnCapture, .switchCell), (.backupDocsToGallery, .switchCell), (.returnStitchedPDF, .switchCell), (.closeCameraOnSubmit, .switchCell), (.locationServicesIsOn, .switchCell), (.originalImageMaxSizeInMB, .doubleValueCell)] let imageProcessingSection: [(Keys,Types)] = [(.autoRotateIsOn, .switchCell),(.autoDocDetectionAndCropIsOn,.switchCell),(.blurDetectionIsOn, .switchCell),(.autoSkewCorrectionIsOn,.switchCell),(.autoCropGalleryIsOn, .switchCell), (.gpuIsOn, .switchCell)] - let uiSection: [(Keys,Types)] = [(.docDetectFillUIColor, .colorCell), (.submitButtonBackgroundColor, .stringColorCell),(.submitButtonBorderColor,.stringColorCell),(.submitButtonFontColor,.stringColorCell),(.docDetectStrokeUIColor,.colorCell),(.submitButtonCornerRadius, .integerValueCell),(.manualCropIsOn,.switchCell),(.moreMenuIsOn,.switchCell),(.moreSettingsMenuIsOn,.switchCell),(.galleryIsOn,.switchCell),(.dictateIsOn,.switchCell),(.emailCCIsOn,.switchCell),(.emailCCDomain,.stringValueCell),(.rotateDocIsOn,.switchCell),(.shieldProtectionIsOn,.switchCell)] + let uiSection: [(Keys,Types)] = [(.docDetectFillUIColor, .stringColorCell), (.submitButtonBackgroundColor, .stringColorCell),(.submitButtonBorderColor,.stringColorCell),(.submitButtonFontColor,.stringColorCell),(.docDetectStrokeUIColor,.stringColorCell),(.submitButtonCornerRadius, .integerValueCell),(.manualCropIsOn,.switchCell),(.moreMenuIsOn,.switchCell),(.moreSettingsMenuIsOn,.switchCell),(.galleryIsOn,.switchCell),(.dictateIsOn,.switchCell),(.emailCCIsOn,.switchCell),(.emailCCDomain,.stringValueCell),(.rotateDocIsOn,.switchCell),(.shieldProtectionIsOn,.switchCell)] let apiSection: [(Keys,Types)] = [(.autoDeleteAfterProcessing, .switchCell),(.boostModeIsOn, .switchCell),(.boundingBoxesIsOn, .switchCell),(.detectBlurResponseIsOn,.switchCell),(.isProduction,.switchCell),(.confidenceDetailsIsOn,.switchCell),(.parseAddressIsOn,.switchCell),(.externalId,.stringValueCell)] lazy var sections: [[(Keys,Types)]] = [generalSection, imageProcessingSection, uiSection, apiSection] @@ -50,9 +50,9 @@ class LensSettingsViewController: UIViewController { //MARK: Cell values handlers func handleColorCellTap(indexPath: IndexPath) { let title = sections[indexPath.section][indexPath.row].0.rawValue - let type = sections[indexPath.section][indexPath.row].1 - let rawColor = type == .colorCell ? jsonSettings[title] as? UIColor ?? UIColor.green.withAlphaComponent(0.3) : UIColor.color(from: jsonSettings[title] as? String ?? "") - guard let color = rawColor else { return } + let color = UIColor.color( + from: jsonSettings[title] as? String ?? "" + ) ?? UIColor.green.withAlphaComponent(0.3) changingColor = sections[indexPath.section][indexPath.row] changingColorIndexPath = indexPath diff --git a/LensW2Demo/Utils/Keys.swift b/LensW2Demo/Utils/Keys.swift index 7e9daa5..5ae9944 100644 --- a/LensW2Demo/Utils/Keys.swift +++ b/LensW2Demo/Utils/Keys.swift @@ -74,6 +74,5 @@ enum Types { case doubleValueCell case integerValueCell case stringValueCell - case colorCell case stringColorCell } diff --git a/Podfile b/Podfile index 9c30f7b..87291b5 100644 --- a/Podfile +++ b/Podfile @@ -8,5 +8,5 @@ target 'LensW2Demo' do use_frameworks! # Pods for LensW2Demo - pod 'VeryfiLens-W2', '2.1.15.1' + pod 'VeryfiLens-W2', '2.1.16.3' end