Advertisement
Guest User

Untitled

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