Skip to content
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
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Modules/RobotKit/Sources/Robot+Gradient.swift
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
}
Comment on lines +15 to +17
Copy link
Member

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

Copy link
Member

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


// 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
Copy link
Member

Choose a reason for hiding this comment

The 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