title: How to change Brightness, Contrast and Saturation using CIColorControls via UISlider with Swift
body: <p>I'm working on a Photo Filter App and as you can see I added a feature to adjust contrast, brightness, saturation, and noise. But the problem is they work independently, which means when I'm adjusting let's say brightness as soon as I start editing contrast, it returns to the original brightness.</p>
  
  <p><a href="https://i.stack.imgur.com/nhlT4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nhlT4.png" alt="Interface"></a></p>
  
  <p>Here is a preview when I'm putting the brightness to the maximum (the image gets white) and then trying to adjust its contrast and the slider changes the contrast of an original image.</p>
  
  <p><a href="https://i.stack.imgur.com/jUcOi.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jUcOi.gif" alt="Filters work independetly!"></a></p>
  
  <p>Here I was able to catch the moment of releasing the slider and putting its value on the original image and as you can see it works, on a demo I put saturation 0 and then change the contrast of the same saturated to 0 picture.</p>
  
  <p><a href="https://i.stack.imgur.com/mtUQo.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mtUQo.gif" alt="Works!"></a>. </p>
  
  <p>The problem is that now when I just click on the slider without changing its value it doubled the current value. For example, if I put the brightness 5 and the put saturation 10 and decided to adjust the brightness as soon as I click on brightness it is doubling its value I don't know why.
  Here is an example. I'm just clicking on slider without changing its value. </p>
  
  <p><a href="https://i.stack.imgur.com/LTqaX.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LTqaX.gif" alt="enter image description here"></a></p>
  
  <p>And here is the code</p>
  
  <pre><code>  @objc func sliderValueDidChange(_ sender: UISlider!) {
  
      if sender.tag == 0 {
  
          let displayinPercentage: Int = Int((sender.value/200) * 10000)
          brightnessValueLabel.text = ("\(displayinPercentage)")
          selectedPictureImageView.image = originalImage
          let beginImage = CIImage(image: selectedPictureImageView.image!)
          self.filter = CIFilter(name: "CIColorControls")
          self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
          self.filter.setValue(sender.value, forKey: kCIInputBrightnessKey)
          self.filteredImage = self.filter?.outputImage
          selectedPictureImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
          sliderValue = sender.value
  
  
      } else if sender.tag == 1 {
  
          let displayinPercentage: Int = Int((sender.value/200) * 10000)
          contrastValueLabel.text = ("\(displayinPercentage)")
          self.selectedPictureImageView.image = originalImage
          let beginImage = CIImage(image: self.selectedPictureImageView.image!)
          self.filter = CIFilter(name: "CIColorControls")
          self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
          self.filter.setValue(sender.value, forKey: kCIInputContrastKey)
          self.filteredImage = self.filter?.outputImage
          self.selectedPictureImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
          sliderValue = sender.value
  
      } else if sender.tag == 2 {
  
          let displayinPercentage: Int = Int((sender.value/200) * 10000)
          saturationValueLabel.text = ("\(displayinPercentage)")
          self.selectedPictureImageView.image = originalImage
          let beginImage = CIImage(image: self.selectedPictureImageView.image!)
          self.filter = CIFilter(name: "CIColorControls")
          self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
          self.filter.setValue(sender.value, forKey: kCIInputSaturationKey)
          self.filteredImage = self.filter?.outputImage
          self.selectedPictureImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
          sliderValue = sender.value
  
      } else if sender.tag == 3 {
  
          let displayinPercentage: Int = Int((sender.value/200) * 10000)
          sharpenValueLabel.text = ("\(displayinPercentage)")
          self.selectedPictureImageView.image = originalImage
          let beginImage = CIImage(image: self.selectedPictureImageView.image!)
          self.filter = CIFilter(name: "CIUnsharpMask")
          self.filter?.setValue(beginImage, forKey: kCIInputImageKey)
          self.filter.setValue(7, forKey: kCIInputRadiusKey)
          self.filter.setValue(sender.value, forKey: kCIInputIntensityKey)
          self.filteredImage = self.filter?.outputImage
          self.selectedPictureImageView.image = UIImage(cgImage: self.context.createCGImage(self.filteredImage!, from: (self.filteredImage?.extent)!)!)
          sliderValue = sender.value
  
      }
  }
  
  @objc func sliderValueDidEnd(_ sender: UISlider!) {
  
      if sender.tag == 0 {
          originalImage = selectedPictureImageView.image
          sender.value = sliderValue
          print("Unpressed button slider value is \(sender.value)")
      } else if sender.tag == 1 {
          originalImage = selectedPictureImageView.image
          sender.value = sliderValue
          print("Unpressed button slider value is \(sender.value)")
      } else if sender.tag == 2 {
          originalImage = selectedPictureImageView.image
          sender.value = sliderValue
          print("Unpressed button slider value is \(sender.value)")
      } else if sender.tag == 3 {
          originalImage = selectedPictureImageView.image
          sender.value = sliderValue
          print("Unpressed button slider value is \(sender.value)")
      }
  }
  </code></pre>
  
  <p>I think that everything is working fine, I just did wrong calculations on filters values.</p>