Skip to content

Commit

Permalink
Split Color into Color and ApplyColor
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasf committed Mar 4, 2024
1 parent 4b5202a commit 892cacf
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 87 deletions.
87 changes: 87 additions & 0 deletions Sources/SwiftSCAD/Development/ApplyColor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import Foundation

internal extension Color {
func call(child: any Geometry) -> SCADCall {
let params: [String: any SCADValue]

switch self {
case .components (let red, let green, let blue, let alpha):
params = ["c": [red, green, blue, alpha]]

case .named (let name, let alpha):
params = ["c": name.rawValue, "alpha": alpha]
}

return SCADCall(name: "color", params: params, body: child)
}
}

struct ApplyColor2D: CoreGeometry2D {
let color: Color
let content: any Geometry2D

func call(in environment: Environment) -> SCADCall {
color.call(child: content)
}
}

extension Geometry2D {
func colored(_ color: Color) -> any Geometry2D {
ApplyColor2D(color: color, content: self)
}

/// Apply a color to the geometry
/// - Parameters:
/// - name: One of the standard colors. See [the OpenSCAD docs](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#color) for more info.
/// - alpha: The alpha value of the color, in the range 0.0 to 1.0.
/// - Returns: A colored geometry
public func colored(_ name: Color.Name, alpha: Double = 1) -> any Geometry2D {
ApplyColor2D(color: .named(name, alpha: alpha), content: self)
}

/// Apply a color to the geometry
/// - Parameters:
/// - red: The red component, in the range 0.0 to 1.0.
/// - green: The green component, in the range 0.0 to 1.0.
/// - blue: The blue component, in the range 0.0 to 1.0.
/// - alpha: The alpha component, in the range 0.0 to 1.0.
/// - Returns: A colored geometry
public func colored(red: Double, green: Double, blue: Double, alpha: Double = 1) -> any Geometry2D {
ApplyColor2D(color: .components(red: red, green: green, blue: blue, alpha: alpha), content: self)
}
}

struct ApplyColor3D: CoreGeometry3D {
let color: Color
let content: any Geometry3D

func call(in environment: Environment) -> SCADCall {
color.call(child: content)
}
}

extension Geometry3D {
func colored(_ color: Color) -> any Geometry3D {
ApplyColor3D(color: color, content: self)
}

/// Apply a color to the geometry
/// - Parameters:
/// - name: One of the standard colors. See [the OpenSCAD docs](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#color) for more info.
/// - alpha: The alpha value of the color, in the range 0.0 to 1.0.
/// - Returns: A colored geometry
public func colored(_ name: Color.Name, alpha: Double = 1) -> any Geometry3D {
ApplyColor3D(color: .named(name, alpha: alpha), content: self)
}

/// Apply a color to the geometry
/// - Parameters:
/// - red: The red component, in the range 0.0 to 1.0.
/// - green: The green component, in the range 0.0 to 1.0.
/// - blue: The blue component, in the range 0.0 to 1.0.
/// - alpha: The alpha component, in the range 0.0 to 1.0.
/// - Returns: A colored geometry
public func colored(red: Double, green: Double, blue: Double, alpha: Double = 1) -> any Geometry3D {
ApplyColor3D(color: .components(red: red, green: green, blue: blue, alpha: alpha), content: self)
}
}
3 changes: 2 additions & 1 deletion Sources/SwiftSCAD/Shapes/2D/Text/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ fileprivate extension Text {
.translated(.init(fragment.rect.origin))

if let color, let scadColor = Color(color) {
Color2D(color: scadColor, content: geometry)
geometry
.colored(scadColor)
} else {
geometry
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,90 +146,4 @@ public enum Color {
case darkSlateGray
case black
}

internal func call(child: any Geometry) -> SCADCall {
let params: [String: any SCADValue]

switch self {
case .components (let red, let green, let blue, let alpha):
params = ["c": [red, green, blue, alpha]]

case .named (let name, let alpha):
params = ["c": name.rawValue, "alpha": alpha]
}

return SCADCall(name: "color", params: params, body: child)
}
}


struct Color3D: CoreGeometry3D {
let color: Color
let content: any Geometry3D

func call(in environment: Environment) -> SCADCall {
color.call(child: content)
}
}

extension Geometry3D {
func colored(_ color: Color) -> any Geometry3D {
Color3D(color: color, content: self)
}

/// Apply a color to the geometry
/// - Parameters:
/// - name: One of the standard colors. See [the OpenSCAD docs](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#color) for more info.
/// - alpha: The alpha value of the color, in the range 0.0 to 1.0.
/// - Returns: A colored geometry
public func colored(_ name: Color.Name, alpha: Double = 1) -> any Geometry3D {
Color3D(color: .named(name, alpha: alpha), content: self)
}

/// Apply a color to the geometry
/// - Parameters:
/// - red: The red component, in the range 0.0 to 1.0.
/// - green: The green component, in the range 0.0 to 1.0.
/// - blue: The blue component, in the range 0.0 to 1.0.
/// - alpha: The alpha component, in the range 0.0 to 1.0.
/// - Returns: A colored geometry
public func colored(red: Double, green: Double, blue: Double, alpha: Double = 1) -> any Geometry3D {
Color3D(color: .components(red: red, green: green, blue: blue, alpha: alpha), content: self)
}
}


struct Color2D: CoreGeometry2D {
let color: Color
let content: any Geometry2D

func call(in environment: Environment) -> SCADCall {
color.call(child: content)
}
}

extension Geometry2D {
func colored(_ color: Color) -> any Geometry2D {
Color2D(color: color, content: self)
}

/// Apply a color to the geometry
/// - Parameters:
/// - name: One of the standard colors. See [the OpenSCAD docs](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#color) for more info.
/// - alpha: The alpha value of the color, in the range 0.0 to 1.0.
/// - Returns: A colored geometry
public func colored(_ name: Color.Name, alpha: Double = 1) -> any Geometry2D {
Color2D(color: .named(name, alpha: alpha), content: self)
}

/// Apply a color to the geometry
/// - Parameters:
/// - red: The red component, in the range 0.0 to 1.0.
/// - green: The green component, in the range 0.0 to 1.0.
/// - blue: The blue component, in the range 0.0 to 1.0.
/// - alpha: The alpha component, in the range 0.0 to 1.0.
/// - Returns: A colored geometry
public func colored(red: Double, green: Double, blue: Double, alpha: Double = 1) -> any Geometry2D {
Color2D(color: .components(red: red, green: green, blue: blue, alpha: alpha), content: self)
}
}

0 comments on commit 892cacf

Please sign in to comment.