Skip to content

Commit

Permalink
Add RGBColor struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkenso committed Jan 30, 2025
1 parent b92382d commit df023c1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Sources/SpellbookFoundation/GUI/RGBColor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// RGBColor.swift
// SwiftSpellbook
//
// Created by Alkenso (Vladimir Vashurkin) on 2025-01-30.
//

import CoreGraphics

#if canImport(CoreGraphics)

public struct RGBColor: Hashable, Codable {
public var red: CGFloat
public var green: CGFloat
public var blue: CGFloat
public var alpha: CGFloat

public init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat = 1.0) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}
}

extension RGBColor {
public var cgColor: CGColor {
CGColor(red: red, green: green, blue: blue, alpha: alpha)
}
}

extension RGBColor {
public static func random(
red: ClosedRange<CGFloat> = 0...1,
green: ClosedRange<CGFloat> = 0...1,
blue: ClosedRange<CGFloat> = 0...1
) -> RGBColor {
.init(red: .random(in: red), green: .random(in: green), blue: .random(in: blue))
}
}

#endif

0 comments on commit df023c1

Please sign in to comment.