Skip to content

Commit 6f591e9

Browse files
author
Rastislav Mirek
committed
property reversePercentage and thumbLabelFormatter added to ColorSliderControl to allow for more control over thumb label (slider percentage).
fixed issue #29
1 parent 5c0a040 commit 6f591e9

File tree

10 files changed

+148
-31
lines changed

10 files changed

+148
-31
lines changed

CHANGELOG.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## 1.4.2 - 2019-5-06
5+
## 1.4.3 - 2020-6-04
6+
7+
### Added
8+
- property `reversePercentage` and `thumbLabelFormatter` added to `ColorSliderControl` and property `reverseBrightnessPercentage` added to `DefaultColorPickerViewController` to allow for more control over thumb label (slider percentage).
9+
- new row added to demo demonstrating use of `reverseBrightnessPercentage` with `DefaultColorPickerViewController`
10+
11+
### Fixed
12+
Issue #29: Is it me or is does brightness work backwards?
13+
14+
## 1.4.2 - 2020-5-06
615

716
### Fixed
817
Issue #27 that prevented compilation of 1.4.1 when installed via SPM.
918

10-
## 1.4.1 - 2019-5-04
19+
## 1.4.1 - 2020-5-04
1120

1221
WARNING: Do not use this realese. It does not compile when istaled via SPM. Use 1.4.2 instead.
1322

1423
### Fixed
1524
Cocoapods pod file version
1625

17-
## 1.4 - 2019-5-03
26+
## 1.4 - 2020-5-03
1827

1928
### Added
2029
- complete support for iOS dark mode

FlexColorPicker.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Pod::Spec.new do |s|
1010
s.name = 'FlexColorPicker'
1111
s.module_name = 'FlexColorPicker'
12-
s.version = '1.4.2'
12+
s.version = '1.4.3'
1313
s.summary = 'Modern & flexible full spectrum color picker written in Swift 5. Supports HSB and RGB color models.'
1414

1515
s.description = <<-DESC

FlexColorPicker/Classes/Controls/ColorSliderControl.swift

+26-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ open class ColorSliderControl: ColorControlWithThumbView {
4242
public let gradientBackgroundView = UIImageView()
4343
/// Previews color options avaialable va chnaging value of the slider in form of linear gradient.
4444
public let gradientView = GradientView()
45+
46+
/// When `true` the thumb shows 100% label for left-most possition of the slider and 0% for right-most possition. Default is `false` (0% is displayed on left). Has no effect if `thumbLabelFormatter` is set.
47+
///
48+
/// This is usefull e.g. when "physically correct" percentage label behaviour of `BrightnessSliderControl` is preferred (as the most "bright" color is on the left of the slider in that case).
49+
public var reversePercentage: Bool = false {
50+
didSet {
51+
let (value, _, _) = sliderDelegate.valueAndGradient(for: selectedHSBColor)
52+
updatePercentageLabel(for: value)
53+
}
54+
}
55+
56+
/// When set to non-nil value it will be used to generate label text of `thumbView` directly instead of via setting `thumbView`s `percentage` property. setting this overrides `percentage` property.
57+
public var thumbLabelFormatter: ((CGFloat) -> String)? {
58+
didSet {
59+
let (value, _, _) = sliderDelegate.valueAndGradient(for: selectedHSBColor)
60+
updatePercentageLabel(for: value)
61+
}
62+
}
4563

4664
/// Whether to display default thin border around the slider.
4765
@IBInspectable
@@ -109,11 +127,18 @@ open class ColorSliderControl: ColorControlWithThumbView {
109127
open override func updateSelectedColor(at point: CGPoint, isInteractive: Bool) {
110128
let gradientLength = contentBounds.width - thumbView.colorIdicatorRadius * 2
111129
let value = max(0, min(1, (point.x - thumbView.colorIdicatorRadius) / gradientLength))
112-
thumbView.percentage = Int(round(value * 100))
130+
updatePercentageLabel(for: value)
113131
setSelectedHSBColor(sliderDelegate.modifiedColor(from: selectedHSBColor, with: min(max(0, value), 1)), isInteractive: isInteractive)
114132
sendActions(for: .valueChanged)
115133
}
116134

135+
public func updatePercentageLabel(for value: CGFloat) {
136+
thumbView.percentage = Int(round((reversePercentage ? 1 - value : value) * 100))
137+
if let thumbLabelFormatter = thumbLabelFormatter {
138+
thumbView.percentageLabel.text = thumbLabelFormatter(value)
139+
}
140+
}
141+
117142
/// Updates border color of the color slider control when interface is changed to dark or light mode.
118143
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
119144
super.traitCollectionDidChange(previousTraitCollection)

FlexColorPicker/Classes/Controls/ComponentSliderControls.swift

+13
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,20 @@ final public class SaturationSliderControl: ColorSliderControl {
4040
/// Color slider that allows to change brightness (in terms of HSB color model) of currently selected color by panning the slider line or by tapping it.
4141
///
4242
/// Tapping left end of the slider will select slider's current color modified to have 100% brightness. Tapping right edge of the slider will select current color modified to have 0% brightness.
43+
///
44+
/// - Note: Unlike other provided sliders, this slider's default thumb label percentage shown does not correspond to actual physical properties of the selected color.
45+
/// When thumb is on lift-most side of the slider it shows 0% while the color brightness is actually 100% (and vice-versa for the right-most thumb position). This is intentional as most users expect such behaviour.
46+
/// If "physically correct" percentage label behaviour is preferred (this is usually the case when you your UI labels this slider as "Brightness:" or when your user base might be more knowleadgable about colors theory) set property `reversePercentage` to true.
4347
final public class BrightnessSliderControl: ColorSliderControl {
48+
49+
/// When `true` the thumb shows 100% label for left-most possition of the slider and 0% for right-most possition. Default is `false` (0% is displayed on left). Has no effect if `thumbLabelFormatter` is set.
50+
///
51+
/// This is usefull when "physically correct" percentage label behaviour is preferred (as the most "bright" color is on the left of the slider).
52+
@IBInspectable
53+
public override var reversePercentage: Bool {
54+
didSet {} // override only to add inspectable
55+
}
56+
4457
public override func commonInit() {
4558
sliderDelegate = BrightnessSliderDelegate()
4659
super.commonInit()

FlexColorPicker/Classes/DefaultColorPickerViewController.swift

+13
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ open class DefaultColorPickerViewController: UIViewController, ColorPickerContro
9191
(colorPalette as? RectangularPaletteControl)?.borderOn = rectangularPaletteBorderOn
9292
}
9393
}
94+
95+
/// When `true` the brightness slider label shows 100% for the left-most possition of the slider thumb and 0% for right-most possition. Default is `false` (0% is displayed on left).
96+
///
97+
/// This is usefull when "physically correct" percentage label behaviour of `BrightnessSliderControl` is preffered (as the most "bright" color is on the left of the slider). However, default value corresponds to the behaviour most users expect.
98+
@IBInspectable
99+
public var reverseBrightnessPercentage: Bool {
100+
get {
101+
return brightnessSlider.reversePercentage
102+
}
103+
set {
104+
brightnessSlider.reversePercentage = newValue
105+
}
106+
}
94107

95108
/// Color picker delegate that gets called when selected color is updated or confirmed. The delegate is not retained. This is just convinience property and getting or setting it is equivalent to getting or setting `colorPicker.delegate`.
96109
open var delegate: ColorPickerDelegate? {

FlexColorPicker/Classes/Views/ColorPickerThumbView.swift

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ private let percentageTextFont = UIFont.monospacedDigitSystemFont(ofSize: 14, we
4545
open class ColorPickerThumbView: UIViewWithCommonInit {
4646
public let borderView: CircleShapedView = LimitedGestureCircleView()
4747
public let colorView: CircleShapedView = LimitedGestureCircleView()
48+
/// The label with thumb text visible while user is repositioning this thumbView.
49+
///
50+
/// Typically used to show slider percentage or other selected value. See also `showPercentage`.
4851
public let percentageLabel = UILabel()
4952
/// When `true` the border automatically darken when color is too bright to be contrast enought with white border.
5053
public var autoDarken: Bool = true
@@ -68,6 +71,9 @@ open class ColorPickerThumbView: UIViewWithCommonInit {
6871
}
6972
private(set) open var color: UIColor = defaultSelectedColor.toUIColor()
7073

74+
/// The percentage displayed in `percentageLabel` unless overriden by dirrectly setting `percentageLabel.text` after setting this property.
75+
///
76+
/// Note: In `ColorSliderControl`s this typically corresponds to value of the slider but must not correspond to actual text displayed in `thumbLabel` if `ColorSliderControl.thumbLabelFormatter` is set.
7177
open var percentage: Int = 0 {
7278
didSet {
7379
updatePercentage(percentage)

FlexColorPicker/FlexColorPicker.xcodeproj/project.pbxproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@
432432
CLANG_ENABLE_MODULES = YES;
433433
CODE_SIGN_IDENTITY = "";
434434
CODE_SIGN_STYLE = Automatic;
435-
CURRENT_PROJECT_VERSION = 23;
435+
CURRENT_PROJECT_VERSION = 24;
436436
DEFINES_MODULE = YES;
437437
DEVELOPMENT_TEAM = UWF3VQ79QJ;
438438
DYLIB_COMPATIBILITY_VERSION = 1;
@@ -446,7 +446,7 @@
446446
"@executable_path/Frameworks",
447447
"@loader_path/Frameworks",
448448
);
449-
MARKETING_VERSION = 1.4.2;
449+
MARKETING_VERSION = 1.4.3;
450450
PRODUCT_BUNDLE_IDENTIFIER = com.typesoft.FlexColorPicker;
451451
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
452452
SKIP_INSTALL = YES;
@@ -463,7 +463,7 @@
463463
CLANG_ENABLE_MODULES = YES;
464464
CODE_SIGN_IDENTITY = "";
465465
CODE_SIGN_STYLE = Automatic;
466-
CURRENT_PROJECT_VERSION = 23;
466+
CURRENT_PROJECT_VERSION = 24;
467467
DEFINES_MODULE = YES;
468468
DEVELOPMENT_TEAM = UWF3VQ79QJ;
469469
DYLIB_COMPATIBILITY_VERSION = 1;
@@ -477,7 +477,7 @@
477477
"@executable_path/Frameworks",
478478
"@loader_path/Frameworks",
479479
);
480-
MARKETING_VERSION = 1.4.2;
480+
MARKETING_VERSION = 1.4.3;
481481
PRODUCT_BUNDLE_IDENTIFIER = com.typesoft.FlexColorPicker;
482482
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
483483
SKIP_INSTALL = YES;

0 commit comments

Comments
 (0)