Skip to content

Commit

Permalink
Merge pull request #2 from intitni/generate-gradient-from-other-color…
Browse files Browse the repository at this point in the history
…space-colors

Generate gradient from other color space colors
  • Loading branch information
intitni authored May 8, 2021
2 parents 5e09303 + c6fb7ff commit f9d4074
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@ let hsbColors = SmoothGradientGenerator()
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: LCHColor(l: 1, c: 1, h: 1, alpha: 1),
to: LCHColor(l: 1, c: 1, h: 1, alpha: 1),
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: HSLColor(h: 1, s: 1, l: 1, alpha: 1),
to: HSLColor(h: 1, s: 1, l: 1, alpha: 1),
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: HSBColor(h: 1, s: 1, b: 1, alpha: 1),
to: HSBColor(h: 1, s: 1, b: 1, alpha: 1),
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: XYZColor(x: 1, y: 1, z: 1, alpha: 1),
to: XYZColor(x: 1, y: 1, z: 1, alpha: 1),
interpolation: .hsb,
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: LABColor(l: 1, a: 1, b: 1, alpha: 1),
to: LABColor(l: 1, a: 1, b: 1, alpha: 1),
interpolation: .hsb,
precision: .high
)

struct ContentView: View {
var body: some View {
VStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@ let hsbColors = SmoothGradientGenerator()
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: LCHColor(l: 1, c: 1, h: 1, alpha: 1),
to: LCHColor(l: 1, c: 1, h: 1, alpha: 1),
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: HSLColor(h: 1, s: 1, l: 1, alpha: 1),
to: HSLColor(h: 1, s: 1, l: 1, alpha: 1),
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: HSBColor(h: 1, s: 1, b: 1, alpha: 1),
to: HSBColor(h: 1, s: 1, b: 1, alpha: 1),
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: XYZColor(x: 1, y: 1, z: 1, alpha: 1),
to: XYZColor(x: 1, y: 1, z: 1, alpha: 1),
interpolation: .hsb,
precision: .high
)

_ = SmoothGradientGenerator().generate(
from: LABColor(l: 1, a: 1, b: 1, alpha: 1),
to: LABColor(l: 1, a: 1, b: 1, alpha: 1),
interpolation: .hsb,
precision: .high
)

struct ContentView: View {
var body: some View {
VStack {
Expand Down
79 changes: 42 additions & 37 deletions Sources/SmoothGradient/ColorSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,17 @@ public struct RGBColor {

return HSBColor(h: h * 360, s: s * 100, b: v * 100, alpha: alpha)
}

func lerp(_ other: RGBColor, t: Double) -> RGBColor {
return RGBColor(
r: r + (other.r - r) * t,
g: g + (other.g - g) * t,
b: b + (other.b - b) * t,
alpha: alpha + (other.alpha - alpha) * t
)
}
}

// MARK: - XYZ

struct XYZColor {
let x: Double // 0..0.95047
let y: Double // 0..1
let z: Double // 0..1.08883
let alpha: Double // 0..1
public struct XYZColor {
public let x: Double // 0..0.95047
public let y: Double // 0..1
public let z: Double // 0..1.08883
public let alpha: Double // 0..1

init(x: Double, y: Double, z: Double, alpha: Double) {
public init(x: Double, y: Double, z: Double, alpha: Double) {
self.x = x
self.y = y
self.z = z
Expand Down Expand Up @@ -217,13 +208,13 @@ struct XYZColor {

// MARK: - LAB

struct LABColor {
let l: Double // 0..100
let a: Double // -128..128
let b: Double // -128..128
let alpha: Double // 0..1
public struct LABColor {
public let l: Double // 0..100
public let a: Double // -128..128
public let b: Double // -128..128
public let alpha: Double // 0..1

init(l: Double, a: Double, b: Double, alpha: Double) {
public init(l: Double, a: Double, b: Double, alpha: Double) {
self.l = l
self.a = a
self.b = b
Expand Down Expand Up @@ -261,13 +252,13 @@ struct LABColor {

// MARK: - LCH

struct LCHColor {
let l: Double // 0..100
let c: Double // 0..128
let h: Double // 0..360
let alpha: Double // 0..1
public struct LCHColor {
public let l: Double // 0..100
public let c: Double // 0..128
public let h: Double // 0..360
public let alpha: Double // 0..1

init(l: Double, c: Double, h: Double, alpha: Double) {
public init(l: Double, c: Double, h: Double, alpha: Double) {
self.l = l
self.c = c
self.h = h
Expand All @@ -292,11 +283,18 @@ struct LCHColor {

// MARK: - HSL

struct HSLColor {
let h: Double // 0..360
let s: Double // 0..100
let l: Double // 0..100
let alpha: Double // 0..1
public struct HSLColor {
public let h: Double // 0..360
public let s: Double // 0..100
public let l: Double // 0..100
public let alpha: Double // 0..1

public init(h: Double, s: Double, l: Double, alpha: Double) {
self.h = h
self.s = s
self.l = l
self.alpha = alpha
}

/// Converts HSL to RGB (https://en.wikipedia.org/wiki/HSL_and_HSV)
func toRGB() -> RGBColor {
Expand Down Expand Up @@ -331,11 +329,18 @@ struct HSLColor {

// MARK: - HSB / HSV

struct HSBColor {
let h: Double // 0..360
let s: Double // 0..100
let b: Double // 0..100
let alpha: Double // 0..1
public struct HSBColor {
public let h: Double // 0..360
public let s: Double // 0..100
public let b: Double // 0..100
public let alpha: Double // 0..1

public init(h: Double, s: Double, b: Double, alpha: Double) {
self.h = h
self.s = s
self.b = b
self.alpha = alpha
}

/// Converts HSB to RGB (https://en.wikipedia.org/wiki/HSL_and_HSV)
func toRGB() -> RGBColor {
Expand Down
Loading

0 comments on commit f9d4074

Please sign in to comment.