Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.51 KB | None | 0 0
  1. //
  2. //  ContentView.swift
  3. //  TheSwiftUI
  4. //
  5. //  Created by Valerio De Rosa on 14.06.19.
  6. //  Copyright © 2019 Incloud Engineering GmbH. All rights reserved.
  7. //
  8.  
  9. import SwiftUI
  10.  
  11. struct ColorSlider : View {
  12.    
  13.     @Binding var value: Double
  14.     var textColor: Color
  15.    
  16.     var body: some View {
  17.         HStack {
  18.             Text("0").color(textColor)
  19.             Slider(value: $value, from: 0.0, through: 1.0)
  20.             Text("255").color(textColor)
  21.         }.padding()
  22.     }
  23. }
  24.  
  25. struct ContentView: View {
  26.    
  27.     let rTarget = Double.random(in: 0..<1)
  28.     let gTarget = Double.random(in: 0..<1)
  29.     let bTarget = Double.random(in: 0..<1)
  30.     @State var rGuess: Double
  31.     @State var gGuess: Double
  32.     @State var bGuess: Double
  33.     @State var showAlert = false
  34.  
  35.    
  36.     func computeScore() -> Int {
  37.         let rDiff = rGuess - rTarget
  38.         let gDiff = gGuess - gTarget
  39.         let bDiff = bGuess - bTarget
  40.         let diff = sqrt(rDiff * rDiff + gDiff * gDiff + bDiff * bDiff)
  41.         return Int((1.0 - diff) * 100.0 + 0.5)
  42.     }
  43.    
  44.     var body: some View {
  45.        
  46.         VStack {
  47.             HStack {
  48.                 // Target color block
  49.                 VStack {
  50.                     Rectangle().foregroundColor(Color(red: rTarget, green: gTarget, blue: bTarget, opacity: 1.0))
  51.                     Text("Match this color")
  52.                 }
  53.                 // Guess color block
  54.                 VStack {
  55.                     Rectangle().foregroundColor(Color(red: rGuess, green: gGuess, blue: bGuess, opacity: 1.0))
  56.                     HStack {
  57.                         Text("R: \(Int(rGuess * 255.0))")
  58.                         Text("G: \(Int(gGuess * 255.0))")
  59.                         Text("B: \(Int(bGuess * 255.0))")
  60.                     }
  61.                 }
  62.             }
  63.            
  64.             Button(action: {
  65.                 self.showAlert = true
  66.             }) {
  67.                 Text("Hit Me!")
  68.             }.presentation($showAlert) {
  69.                 Alert(title: Text("Your Score"), message: Text("\(computeScore())"))
  70.             }.padding()
  71.            
  72.             VStack {
  73.                 ColorSlider(value: $rGuess, textColor: .red)
  74.                 ColorSlider(value: $gGuess, textColor: .green)
  75.                 ColorSlider(value: $bGuess, textColor: .blue)
  76.             }
  77.         }
  78.     }
  79. }
  80.  
  81. #if DEBUG
  82. struct ContentView_Previews : PreviewProvider {
  83.     static var previews: some View {
  84.         ContentView(rGuess: 0.5, gGuess: 0.5, bGuess: 0.5)
  85.     }
  86. }
  87. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement