Skip to content

Commit

Permalink
add TintColorPicker
Browse files Browse the repository at this point in the history
  • Loading branch information
徐水峰 authored and xu.shuifeng committed Jun 14, 2018
1 parent 2aa2a74 commit 8a51f7f
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 24 deletions.
4 changes: 4 additions & 0 deletions MetalFilters.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
6C6C88BFBC95C7EDDE5BA9ED /* Pods_MetalFilters.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B817D3CCD2892291BA74BE47 /* Pods_MetalFilters.framework */; };
A30D742120D0EC1C00824A1A /* MTLocalLaplacianFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A30D742020D0EC1C00824A1A /* MTLocalLaplacianFilter.swift */; };
A30D742320D0EC2A00824A1A /* MTLocalLaplacianFilter.metal in Sources */ = {isa = PBXBuildFile; fileRef = A30D742220D0EC2A00824A1A /* MTLocalLaplacianFilter.metal */; };
A32F2BF420D2393E0018C281 /* FilterTintColorPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = A32F2BF320D2393E0018C281 /* FilterTintColorPicker.swift */; };
A36133C120C665A70022F27C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A36133C020C665A70022F27C /* AppDelegate.swift */; };
A36133C620C665A70022F27C /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A36133C420C665A70022F27C /* Main.storyboard */; };
A36133C820C665A70022F27C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A36133C720C665A70022F27C /* Assets.xcassets */; };
Expand Down Expand Up @@ -119,6 +120,7 @@
8CF694358EC8CD79BA5C6B8E /* Pods-MetalFilters.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MetalFilters.release.xcconfig"; path = "Pods/Target Support Files/Pods-MetalFilters/Pods-MetalFilters.release.xcconfig"; sourceTree = "<group>"; };
A30D742020D0EC1C00824A1A /* MTLocalLaplacianFilter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MTLocalLaplacianFilter.swift; sourceTree = "<group>"; };
A30D742220D0EC2A00824A1A /* MTLocalLaplacianFilter.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = MTLocalLaplacianFilter.metal; sourceTree = "<group>"; };
A32F2BF320D2393E0018C281 /* FilterTintColorPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterTintColorPicker.swift; sourceTree = "<group>"; };
A36133BD20C665A70022F27C /* MetalFilters.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MetalFilters.app; sourceTree = BUILT_PRODUCTS_DIR; };
A36133C020C665A70022F27C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
A36133C520C665A70022F27C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -423,6 +425,7 @@
A38FC79620CF40F8002CA905 /* SloppyTouchSlider.swift */,
A3A068DE20CFA92F00A2DF76 /* FilterControlView.swift */,
A3A068E420CFCE3800A2DF76 /* HorizontalSliderView.swift */,
A32F2BF320D2393E0018C281 /* FilterTintColorPicker.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -562,6 +565,7 @@
A38FC65A20CB7979002CA905 /* MainViewController.swift in Sources */,
A3A068E520CFCE3800A2DF76 /* HorizontalSliderView.swift in Sources */,
A391B2F020CE4CCA00E6FD67 /* MTJunoFilter.metal in Sources */,
A32F2BF420D2393E0018C281 /* FilterTintColorPicker.swift in Sources */,
A391B2DF20CE4CCA00E6FD67 /* MTToasterFilter.metal in Sources */,
A391B31320CE4CCA00E6FD67 /* MTReyesFilter.metal in Sources */,
A391B2EA20CE4CCA00E6FD67 /* MTCremaFilter.swift in Sources */,
Expand Down
49 changes: 30 additions & 19 deletions MetalFilters/MTFilter/MTBasicAdjustFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,39 @@ class MTBasicAdjustFilter: MTFilter {
]
}

func t() {
var knots1: [float2] = []
knots1.append(float2(0, 0))
knots1.append(float2(0.0612549, 0.185368))
knots1.append(float2(0.16381, 0.365771))
knots1.append(float2(0.320955, 0.527539))
knots1.append(float2(0.496851, 0.659237))
knots1.append(float2(0.709977, 0.79987))
knots1.append(float2(1, 1))
var spline1: MTIRGBToneCurveFilter {
var knots: [MTIVector] = []
knots.append(MTIVector(float2: float2(0, 0)))
knots.append(MTIVector(float2: float2(0.0612549, 0.185368)))
knots.append(MTIVector(float2: float2(0.16381, 0.365771)))
knots.append(MTIVector(float2: float2(0.320955, 0.527539)))
knots.append(MTIVector(float2: float2(0.496851, 0.659237)))
knots.append(MTIVector(float2: float2(0.709977, 0.79987)))
knots.append(MTIVector(float2: float2(1, 1)))
let curve = MTIRGBToneCurveFilter()
curve.rgbCompositeControlPoints = knots
return curve
}

var spline2: MTIRGBToneCurveFilter {
var knots: [MTIVector] = []
knots.append(MTIVector(float2: float2(0, 0)))
knots.append(MTIVector(float2: float2(0.185368, 0.0612549)))
knots.append(MTIVector(float2: float2(0.365771, 0.16381)))
knots.append(MTIVector(float2: float2(0.527539, 0.320955)))
knots.append(MTIVector(float2: float2(0.659237, 0.496851)))
knots.append(MTIVector(float2: float2(0.79987, 0.709977)))
knots.append(MTIVector(float2: float2(1, 1)))
let curve = MTIRGBToneCurveFilter()
curve.rgbCompositeControlPoints = knots
return curve
}

override func modifySamplersIfNeeded(_ samplers: [MTIImage]) -> [MTIImage] {

var knots2: [float2] = []
knots2.append(float2(0, 0))
knots2.append(float2(0.185368, 0.0612549))
knots2.append(float2(0.365771, 0.16381))
knots2.append(float2(0.527539, 0.320955))
knots2.append(float2(0.659237, 0.496851))
knots2.append(float2(0.79987, 0.709977))
knots2.append(float2(1, 1))

let curve1 = MTIRGBToneCurveFilter()

let curve2 = MTIRGBToneCurveFilter()
return samplers
}

override var parameters: [String: Any] {
Expand Down
18 changes: 15 additions & 3 deletions MetalFilters/MTFilter/MTFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ class MTFilter: NSObject, MTIUnaryFilter {
class var name: String { return "" }

var borderName: String { return "" }


/// fragment shader name in Metal
var fragmentName: String { return "" }

/// Textures, key should match parameter name
var samplers: [String: String] { return [:] }

var parameters: [String: Any] { return [:] }

/// override this function to modifiy samplers if needed
///
/// - Returns: final samplers passes into Metal
func modifySamplersIfNeeded(_ samplers: [MTIImage]) -> [MTIImage] {
return samplers
}

// MARK: - MTIUnaryFilter

Expand All @@ -35,15 +44,17 @@ class MTFilter: NSObject, MTIUnaryFilter {
return inputImage
}

var images: [MTIImage] = [input]

var images: [MTIImage] = []
for key in samplers.keys.sorted() {
let imageName = samplers[key]!
if imageName.count > 0 {
let image = samplerImage(named: imageName)!
images.append(image)
}
}
images = modifySamplersIfNeeded(images)
images.insert(input, at: 0)

let outputDescriptors = [
MTIRenderPassOutputDescriptor(dimensions: MTITextureDimensions(cgSize: input.size), pixelFormat: outputPixelFormat)
]
Expand All @@ -61,6 +72,7 @@ class MTFilter: NSObject, MTIUnaryFilter {
if let imageUrl = MTFilterManager.shard.url(forResource: name) {
let ciImage = CIImage(contentsOf: imageUrl)
return MTIImage(ciImage: ciImage!, isOpaque: true)
// contentsOf can not load Grayscale texture correctly
// return MTIImage(contentsOf: imageUrl, options: [.SRGB: false], alphaType: .alphaIsOne)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions MetalFilters/MTFilter/MTFilterManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class MTFilterManager {

var allFilters: [MTFilter.Type] = []

var resourceBundle: Bundle
private var resourceBundle: Bundle

var context: MTIContext?
private var context: MTIContext?

init() {

Expand Down
27 changes: 27 additions & 0 deletions MetalFilters/MTFilter/MTTintColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ enum MTTintColor {
}
}

var displayName: String {
switch self {
case .none:
return "No Color"
case .yellow:
return "Yellow"
case .orange:
return "Orange"
case .red:
return "Red"
case .pink:
return "Pink"
case .purple:
return "Purple"
case .blue:
return "Blue"
case .lightBlue:
return "Light Blue"
case .green:
return "Green"
}
}

var colorVector: MTIVector {
switch self {
case .none:
Expand All @@ -65,4 +88,8 @@ enum MTTintColor {
return MTIVector(float3: float3(0.0, 1.0, 0.0))
}
}

static func colors() -> [MTTintColor] {
return [.none, .yellow, .orange, .red, .pink, .purple, .blue, .lightBlue, .green]
}
}
74 changes: 74 additions & 0 deletions MetalFilters/Views/FilterTintColorPicker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// FilterTintColorPicker.swift
// MetalFilters
//
// Created by xu.shuifeng on 2018/6/14.
// Copyright © 2018 shuifeng.me. All rights reserved.
//

import UIKit

protocol FilterTintColorPickerDelegate {

}

class FilterTintColorPicker: UIControl, UIGestureRecognizerDelegate {

private var boundingBoxes: [CGRect] = []

private let padding: CGFloat = 30.0

private let itemSize: CGFloat = 16.0

override init(frame: CGRect) {

super.init(frame: frame)

self.backgroundColor = .clear

var elements: [UIAccessibilityElement] = []

let colors = MTTintColor.colors()
let numberOfColors = colors.count
let width = frame.width - 2*padding
let spaceWidth = (width - CGFloat(numberOfColors)*itemSize)/CGFloat(numberOfColors-1)
for index in 0..<numberOfColors {
let color = colors[index]
let x = padding + CGFloat(index)*(spaceWidth + itemSize)
let rect = CGRect(x: x, y: 75, width: itemSize, height: itemSize)
boundingBoxes.append(rect)

let accessibilityElement = UIAccessibilityElement(accessibilityContainer: self)
accessibilityElement.isAccessibilityElement = true
accessibilityElement.accessibilityFrame = rect
accessibilityElement.accessibilityLabel = color.displayName
accessibilityElement.accessibilityHint = "Tap to apply this color. Tap again to adjust strength."
elements.append(accessibilityElement)
}
self.accessibilityElements = elements
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
addGestureRecognizer(tapGesture)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
super.layoutSubviews()
}

override func draw(_ rect: CGRect) {
// Drawing code
// TODO
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
// layoutIfNeeded
}

@objc private func handleTapGesture(_ gesture: UITapGestureRecognizer) {
//let point = gesture.location(in: self)

}
}

0 comments on commit 8a51f7f

Please sign in to comment.