-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hugo/feature/Add Gradient class #560
Draft
HPezz
wants to merge
5
commits into
develop
Choose a base branch
from
hugo/feature/Add-Gradient-class
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
adc936f
:technologist: (RobotKit): Extend UIColor with toHsv() function
HPezz 485abee
:recycle: (RobotKit): Update Robot.Color to future Gradient struct
HPezz 40c803f
:sparkles: (RobotKit): Add Robot.Gradient struct
HPezz d16e286
:bug: (GEK): Add reinforcer duration
HPezz 450a2c8
:recycle: (GEK): Refactor Pairing breathe animation with Gradient
HPezz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Leka - iOS Monorepo | ||
// Copyright APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import SwiftUI | ||
|
||
// MARK: - Robot.Color | ||
|
||
// swiftlint:disable identifier_name | ||
|
||
public extension Robot { | ||
struct Gradient { | ||
// MARK: Lifecycle | ||
|
||
public init(fromColors colors: Color...) { | ||
self.gradientColors = colors | ||
} | ||
|
||
// MARK: Public | ||
|
||
public func color(at position: Float) -> Robot.Color { | ||
let positionClamped = max(min(position, 1), 0) | ||
|
||
let scaledIndex = positionClamped * Float(self.gradientColors.count - 1) | ||
let firstIndex = Int(scaledIndex) | ||
let secondIndex = min(firstIndex + 1, gradientColors.count - 1) | ||
let fraction = CGFloat(scaledIndex - Float(firstIndex)) | ||
|
||
let firstColor = self.gradientColors[firstIndex] | ||
let secondColor = self.gradientColors[secondIndex] | ||
|
||
let (h1, s1, v1) = firstColor.robotUiColor.toHSV() | ||
let (h2, s2, v2) = secondColor.robotUiColor.toHSV() | ||
|
||
let h = self.interpolateHue(from: h1, to: h2, fraction: fraction) | ||
let s = self.interpolate(from: s1, to: s2, fraction: fraction) | ||
let v = self.interpolate(from: v1, to: v2, fraction: fraction) | ||
|
||
let uiColor = UIColor(hue: h, saturation: s, brightness: v, alpha: 1) | ||
|
||
let r = UInt8(max(min(uiColor.cgColor.components![0] * 255.0, 255), 0)) | ||
let g = UInt8(max(min(uiColor.cgColor.components![1] * 255.0, 255), 0)) | ||
let b = UInt8(max(min(uiColor.cgColor.components![2] * 255.0, 255), 0)) | ||
|
||
return Robot.Color(red: r, green: g, blue: b) | ||
} | ||
Comment on lines
+21
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. par curiosité, cette formule elle vient d'où? c'est intéressant de mettre en commentaire la source de l'explication scientifique/mathématiques du calcul |
||
|
||
// MARK: Private | ||
|
||
private let gradientColors: [Robot.Color] | ||
|
||
private func interpolate(from start: CGFloat, to end: CGFloat, fraction: CGFloat) -> CGFloat { | ||
start + (end - start) * fraction | ||
} | ||
|
||
private func interpolateHue(from start: CGFloat, to end: CGFloat, fraction: CGFloat) -> CGFloat { | ||
let diff = abs(end - start) | ||
if diff > 0.5 { | ||
if start > end { | ||
return (start + ((1.0 + end - start) * fraction)).truncatingRemainder(dividingBy: 1.0) | ||
} else { | ||
return (start - ((1.0 + start - end) * fraction)).truncatingRemainder(dividingBy: 1.0) | ||
} | ||
} else { | ||
return self.interpolate(from: start, to: end, fraction: fraction) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// swiftlint:enable identifier_name |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
j'ai un "petit" soucis avec ça : c'est que si tu mets + de 2 couleurs, tu pars du principe qu'elles sont toutes équidistante les unes des autres.
dans Illustrator, tu peux faire varier la distance entre 2 couleurs.
dans notre cas ça peut être très pratique pour "accélérer/raccourcir" les zones dans lesquelles les variations sont faibles (ou faiblement perçues) et "ralentir/allonger" des zones où la variation est plus importante
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
une manière simple est d'avoir un autre init qui te permet d'associer couleur et position