Four color gradients! Standard APIs! Wowwwww

This commit is contained in:
Eryn Wells 2018-10-09 17:44:07 -07:00
parent 141eee69ee
commit 2fa9b838f7
5 changed files with 35 additions and 34 deletions

View file

@ -13,7 +13,7 @@ class ViewController: NSViewController, RendererDelegate {
private static func defaultParameters() -> Parameters {
var p = Parameters()
let defaults = UserDefaults.standard
let style = defaults.colorStyle ?? .gradient2Horizontal
let style = defaults.colorStyle ?? .gradient2
p.colorStyle = style
let color0 = defaults.color0 ?? Float4(0.50, 0.79, 1, 1)
p.color0 = color0

View file

@ -16,14 +16,10 @@ public enum MetaballsError: Error {
public enum ColorStyle: UInt32 {
/// Single flat color
case singleColor = 1
/// Two color horizontal gradient
case gradient2Horizontal = 2
/// Two color vertical gradient
case gradient2Vertical = 3
/// Four color gradient from corners
case gradient4Corners = 4
/// Four color gradient from middle of sides
case gradient4Sides = 5
/// Two color gradient
case gradient2 = 2
/// Four color gradient
case gradient4 = 4
}
public struct Parameters {

View file

@ -25,15 +25,17 @@ public class PreferencesViewController: NSViewController {
return [
StyleItem(name: NSLocalizedString("Single Color", comment: "single color menu item"),
tag: Int(ColorStyle.singleColor.rawValue),
colorNames: [NSLocalizedString("Color", comment: "single color name")]),
StyleItem(name: NSLocalizedString("Two Color Gradient — Horizontal", comment: "two color horizontal gradient menu item"),
tag: Int(ColorStyle.gradient2Horizontal.rawValue),
colorNames: [NSLocalizedString("Right", comment: "two color horizontal gradient, color 1"),
NSLocalizedString("Left", comment: "two color horizontal gradient, color 2")]),
StyleItem(name: NSLocalizedString("Two Color Gradient — Vertical", comment: "two color vertical gradient menu item"),
tag: Int(ColorStyle.gradient2Vertical.rawValue),
colorNames: [NSLocalizedString("Top", comment: "two color vertical gradient, color 1"),
NSLocalizedString("Bottom", comment: "two color vertical gradient, color 2")]),
colorNames: [NSLocalizedString("A", comment: "first color")]),
StyleItem(name: NSLocalizedString("Two Color Gradient", comment: "two color gradient menu item"),
tag: Int(ColorStyle.gradient2.rawValue),
colorNames: [NSLocalizedString("A", comment: "first color"),
NSLocalizedString("B", comment: "second color")]),
StyleItem(name: NSLocalizedString("Four Color Gradient", comment: "four color gradient menu item"),
tag: Int(ColorStyle.gradient4.rawValue),
colorNames: [NSLocalizedString("A", comment: "first color"),
NSLocalizedString("B", comment: "second color"),
NSLocalizedString("C", comment: "third color"),
NSLocalizedString("D", comment: "fourth color")]),
]
}

View file

@ -27,14 +27,10 @@ typedef struct {
typedef enum {
/// Single flat color
SingleColor = 1,
/// Two color horizontal gradient
Gradient2Horizontal = 2,
/// Two color vertical gradient
Gradient2Vertical = 3,
/// Four color gradient from corners
Gradient4Corners = 4,
/// Four color gradient from middle of sides
Gradient4Sides = 5,
/// Two color gradient
Gradient2 = 2,
/// Four color gradient
Gradient4 = 4,
} ColorStyle;
typedef struct {
@ -88,19 +84,26 @@ sampleToColorShader(RasterizerData in [[stage_in]],
case SingleColor:
out = singleColor(sample, target, feather, parameters.colors[0]);
break;
case Gradient2Horizontal: {
const float3 transformedColor = (parameters.colorTransform * float3(in.position.xy, 1.0));
case Gradient2: {
const float3 transformedColor = parameters.colorTransform * float3(in.position.xy, 1.0);
const float blend = transformedColor.x / parameters.size[0];
out = gradient2(sample, target, feather, blend, parameters.colors[0], parameters.colors[1]);
const float4 color = mix(parameters.colors[0], parameters.colors[1], blend);
out = singleColor(sample, target, feather, color);
break;
}
case Gradient2Vertical: {
const float blend = in.position.y / parameters.size[1];
out = gradient2(sample, target, feather, blend, parameters.colors[0], parameters.colors[1]);
case Gradient4: {
const float3 transformedColorCoords = parameters.colorTransform * float3(in.position.xy, 1.0);
const float2 blend = float2(transformedColorCoords.x / parameters.size[0],
transformedColorCoords.y / parameters.size[1]);
const float4 color = mix(mix(parameters.colors[0], parameters.colors[2], blend.y),
mix(parameters.colors[1], parameters.colors[3], blend.y),
blend.x);
out = singleColor(sample, target, feather, color);
break;
}
}
return out;
}
@ -147,7 +150,7 @@ gradient2(float sample,
float4 fromColor,
float4 toColor)
{
float4 blendedColor = averageTwoColors(normalizedBlend, fromColor, toColor);
float4 blendedColor = mix(fromColor, toColor, normalizedBlend);
float4 out = singleColor(sample, target, feather, blendedColor);
return out;
}

View file

@ -14,7 +14,7 @@ import ScreenSaver
private static func defaultParameters() -> Parameters {
var p = Parameters()
let defaults = UserDefaults.standard
let style = defaults.colorStyle ?? .gradient2Horizontal
let style = defaults.colorStyle ?? .gradient2
p.colorStyle = style
let color0 = defaults.color0 ?? Float4(0.50, 0.79, 1, 1)
p.color0 = color0