-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Color into Color and ApplyColor
- Loading branch information
Showing
3 changed files
with
89 additions
and
87 deletions.
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,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) | ||
} | ||
} |
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
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