Skip to content

Commit

Permalink
Merge pull request #25 from chrs1885/feature/24-fix-hex-to-hsb
Browse files Browse the repository at this point in the history
Fix Hex value computation
  • Loading branch information
chrs1885 committed Apr 19, 2020
2 parents 121a626 + ba0a812 commit 8469162
Show file tree
Hide file tree
Showing 30 changed files with 23 additions and 31 deletions.
5 changes: 1 addition & 4 deletions Example/Tests/GrayscaleViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,8 @@ class GrayscaleViewModelTests: QuickSpec {
}

context("when calling hexValueChanged()") {
var testColor: UIColor!

beforeEach {
testColor = .red
sut.hexValueChanged(withColor: testColor)
sut.hexValueChanged(to: "ABCDEF")
}

it("informs the viewDelegate") {
Expand Down
5 changes: 1 addition & 4 deletions Example/Tests/HSBViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,8 @@ class HSBViewModelTests: QuickSpec {
}

context("when calling hexValueChanged()") {
var testColor: UIColor!

beforeEach {
testColor = .red
sut.hexValueChanged(withColor: testColor)
sut.hexValueChanged(to: "ABCDEF")
}

it("informs the viewDelegate") {
Expand Down
5 changes: 1 addition & 4 deletions Example/Tests/RGBViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,8 @@ class RGBViewModelTests: QuickSpec {
}

context("when calling hexValueChanged()") {
var testColor: UIColor!

beforeEach {
testColor = .red
sut.hexValueChanged(withColor: testColor)
sut.hexValueChanged(to: "ABCDEF")
}

it("informs the viewDelegate") {
Expand Down
2 changes: 1 addition & 1 deletion Example/Tests/SheetyColorsViewModelMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ class SheetyColorsViewModelMock: SheetyColorsViewModelProtocol {

func sliderValueChanged(forSliderAt _: Int, value _: CGFloat) {}

func hexValueChanged(withColor _: UIColor) {}
func hexValueChanged(to _: String) {}
}
8 changes: 4 additions & 4 deletions Example/Tests/UIColor+grayscaleColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class UIColorGrayscaleColorTests: QuickSpec {
sut = UIColor(white: 0.5, alpha: 0.5).grayscaleColor
}

it("returns an HSBAColor instance") {
expect(sut.white).to(equal(127.0))
it("returns an GrayscaleColor instance") {
expect(sut.white).to(equal(127.5))
expect(sut.alpha).to(equal(50.0))
}
}
Expand All @@ -32,7 +32,7 @@ class UIColorGrayscaleColorTests: QuickSpec {
}

it("returns an GraycaleColor instance") {
expect(sut.white).to(equal(72.0))
expect(sut.white).to(equal(72.624))
expect(sut.alpha).to(equal(50.0))
}
}
Expand All @@ -43,7 +43,7 @@ class UIColorGrayscaleColorTests: QuickSpec {
}

it("returns an GraycaleColor instance") {
expect(sut.white).to(equal(72.0))
expect(sut.white).to(equal(72.624))
expect(sut.alpha).to(equal(100.0))
}
}
Expand Down
4 changes: 2 additions & 2 deletions Example/Tests/UIColor+hsbaColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class UIColorHsbaColorTests: QuickSpec {

context("when calling hsbaColor on a hsb colorspace color") {
beforeEach {
sut = UIColor(hue: 0.1, saturation: 0.25, brightness: 0.75, alpha: 0.5).hsbaColor
sut = UIColor(hue: 0.25, saturation: 0.25, brightness: 0.75, alpha: 0.5).hsbaColor
}

it("returns an HSBAColor instance") {
expect(sut.hue).to(equal(36.0))
expect(sut.hue).to(equal(90.0))
expect(sut.saturation).to(equal(25.0))
expect(sut.brightness).to(equal(75.0))
expect(sut.alpha).to(equal(50.0))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ extension CGFloat {
mutating func normalizeTo(max: CGFloat) {
self *= max
constrainTo(max: max)
round(.down)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import CoreGraphics
import UIKit

protocol SheetyColorsViewModelProtocol {
var viewDelegate: SheetyColorsViewDelegate? { get set }
Expand All @@ -29,5 +28,5 @@ protocol SheetyColorsViewModelProtocol {
func thumbText(forSliderAt index: Int) -> String?
func thumbIconName(forSliderAt index: Int) -> String?
func sliderValueChanged(forSliderAt index: Int, value: CGFloat)
func hexValueChanged(withColor color: UIColor)
func hexValueChanged(to value: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ extension SheetyColorsViewController: SheetyColorsViewDelegate {

extension SheetyColorsViewController: PreviewColorViewDelegate {
func previewColorView(_: PreviewColorView, didEditHexValue value: String) {
guard let color = UIColor(hex: value) else { return }

viewModel.hexValueChanged(withColor: color)
viewModel.hexValueChanged(to: value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ extension GrayscaleViewModel: SheetyColorsViewModelProtocol {
delegate?.didSelectColor(colorModel.uiColor)
}

func hexValueChanged(withColor color: UIColor) {
func hexValueChanged(to value: String) {
guard let color = UIColor(hex: value) else { return }

colorModel = color.grayscaleColor
viewDelegate?.didUpdateColorComponent(in: self, shouldAnimate: true)
delegate?.didSelectColor(colorModel.uiColor)
Expand Down
5 changes: 2 additions & 3 deletions SheetyColors/Classes/HSB/Models/HSBAColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import UIKit
/// A model class representing HSBA colors. The hue component can hold values between 0.0 and 360.0 while the saturation and brightnes values have a maximum value of 100.0.
public class HSBAColor: NSObject, NSCopying, Codable {
var hue, saturation, brightness, alpha: CGFloat

var hexColor: String {
if let colorRef = uiColor.cgColor.components {
let red: CGFloat = colorRef[0]
let green: CGFloat = colorRef[1]
let blue: CGFloat = colorRef[2]
let rgb: Int = Int(red * 255.0) << 16 | Int(green * 255.0) << 8 | Int(blue * 255.0) << 0

return String(format: "%06x", rgb).uppercased()
return String(format: "%02lX%02lX%02lX", lroundf(Float(red * 255)), lroundf(Float(green * 255)), lroundf(Float(blue * 255))).uppercased()
}

return ""
Expand Down
4 changes: 3 additions & 1 deletion SheetyColors/Classes/HSB/ViewModels/HSBViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ extension HSBViewModel: SheetyColorsViewModelProtocol {
delegate?.didSelectColor(colorModel.uiColor)
}

func hexValueChanged(withColor color: UIColor) {
func hexValueChanged(to value: String) {
guard let color = UIColor(hex: value) else { return }

colorModel = color.hsbaColor
viewDelegate?.didUpdateColorComponent(in: self, shouldAnimate: true)
delegate?.didSelectColor(colorModel.uiColor)
Expand Down
4 changes: 3 additions & 1 deletion SheetyColors/Classes/RGB/ViewModels/RGBViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ extension RGBViewModel: SheetyColorsViewModelProtocol {
delegate?.didSelectColor(colorModel.uiColor)
}

func hexValueChanged(withColor color: UIColor) {
func hexValueChanged(to value: String) {
guard let color = UIColor(hex: value) else { return }

colorModel = color.rgbaColor
viewDelegate?.didUpdateColorComponent(in: self, shouldAnimate: true)
delegate?.didSelectColor(colorModel.uiColor)
Expand Down

0 comments on commit 8469162

Please sign in to comment.