-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start implementation of support for ColorPicker Key in custom k…
…eyboard issue #12
- Loading branch information
Build Pipeline
committed
Jan 5, 2023
1 parent
bcee031
commit f919613
Showing
5 changed files
with
181 additions
and
85 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLKeyboard+Color.swift
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,81 @@ | ||
// | ||
// PlantUMLKeyboard+Color.swift | ||
// | ||
// | ||
// Created by Bartolomeo Sorrentino on 05/01/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// MARK: Color extension | ||
extension Color { | ||
|
||
typealias RGBA = (R:Int, G:Int, B:Int, A:Int ) | ||
|
||
func hexValue() -> RGBA? { | ||
var output:RGBA? = nil | ||
|
||
if let values = self.cgColor?.components { | ||
|
||
switch values.count { | ||
case 1: | ||
output = ( Int(values[0] * 255), Int(values[0] * 255), Int(values[0] * 255),1) | ||
break | ||
case 2: | ||
output = ( Int(values[0] * 255), Int(values[0] * 255), Int(values[0] * 255),Int(values[1] * 255)) | ||
break | ||
case 3: | ||
output = ( Int(values[0] * 255), Int(values[1] * 255), Int(values[2] * 255),1) | ||
case 4: | ||
output = ( Int(values[0] * 255), Int(values[1] * 255), Int(values[2] * 255),Int(values[3] * 255)) | ||
default: | ||
break | ||
} | ||
} | ||
|
||
return output | ||
} | ||
|
||
func hexValue() -> String? { | ||
var output:String? = nil | ||
|
||
if let rgba:RGBA = self.hexValue() { | ||
output = "#\(String(format:"%02X", rgba.R))\(String(format:"%02X", rgba.G))\(String(format:"%02X", rgba.B))\( String(format:"%02X", rgba.A))" | ||
} | ||
|
||
return output | ||
} | ||
} | ||
|
||
struct ColorKeyView: View { | ||
@State private var selectedColor = Color.blue.opacity(0.5) | ||
|
||
var symbol:Symbol | ||
var onPressSymbol: (Symbol) -> Void | ||
|
||
var body: some View { | ||
VStack { | ||
|
||
ColorPicker( selection: $selectedColor, label: { | ||
Text(symbol.id).font(.system(size: 16).bold()) | ||
|
||
}) | ||
.onChange(of: selectedColor ) { color in | ||
onPressSymbol( makeSymbol( from: color ) ) | ||
} | ||
} | ||
} | ||
|
||
private func makeSymbol( from color: Color ) -> Symbol { | ||
|
||
let value = String(format: symbol.value, color.hexValue() ?? "") | ||
return Symbol( id: symbol.id, value: value ) | ||
} | ||
} | ||
|
||
|
||
//struct ColorPickerKeyView_Previews: PreviewProvider { | ||
// static var previews: some View { | ||
// ColorPickerKeyView() | ||
// } | ||
//} |
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
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