From fd1e9bcaf2fc4a620c7902243df1115306c43c70 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:03:57 +0900 Subject: [PATCH] feat: add paste color/gradient button (from css code) close #389 --- Packages/src/Editor/Icons/PasteIcon.png | Bin 0 -> 1135 bytes Packages/src/Editor/Icons/PasteIcon.png.meta | 109 +++++++ Packages/src/Editor/Icons/d_PasteIcon.png | Bin 0 -> 522 bytes .../src/Editor/Icons/d_PasteIcon.png.meta | 109 +++++++ Packages/src/Editor/PasteColorUtil.cs | 146 +++++++++ Packages/src/Editor/PasteColorUtil.cs.meta | 11 + Packages/src/Editor/PasteGradientUtil.cs | 293 ++++++++++++++++++ Packages/src/Editor/PasteGradientUtil.cs.meta | 11 + Packages/src/Editor/UIEffectPropertyEditor.cs | 32 +- 9 files changed, 706 insertions(+), 5 deletions(-) create mode 100644 Packages/src/Editor/Icons/PasteIcon.png create mode 100644 Packages/src/Editor/Icons/PasteIcon.png.meta create mode 100644 Packages/src/Editor/Icons/d_PasteIcon.png create mode 100644 Packages/src/Editor/Icons/d_PasteIcon.png.meta create mode 100644 Packages/src/Editor/PasteColorUtil.cs create mode 100644 Packages/src/Editor/PasteColorUtil.cs.meta create mode 100644 Packages/src/Editor/PasteGradientUtil.cs create mode 100644 Packages/src/Editor/PasteGradientUtil.cs.meta diff --git a/Packages/src/Editor/Icons/PasteIcon.png b/Packages/src/Editor/Icons/PasteIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..9f389587ca0dd3b20df46f357df6ec2751737c5b GIT binary patch literal 1135 zcmV-#1d#iQP)pF8FWQhbW?9;ba!ELWdL_~cP?peYja~^aAhuUa%Y?FJQ@H11N=!u zK~#90?VHVOV^tKue>YD|VG^b2LY>Y(U<7ql6#WAx)L ziW6of4ry)(f*+l&AIR9vAZY&q+o`xww05B-IWC%!H?OI=ukYh_==(t+_r81YIrrqe zd+s|o&!{S=nUZe>d~@mza95Uti^#pebE;YcFve^GpIK}F$Wm?`Ojc`vh%5t_fNJE@ zt}*7VUa$8-CW2#<$t}=qHs1kOI6QfPs;aKGTCKOUkR6xQJwQaB0KNmvl%`rLm7ZK% zTl+p$1!9uA2dL^rZlC-v@ZR|ISE$~MMx%>q%a2Q94-kh*fLTCFar z>Wjc5z!`^bzglY_2v>;6Z@{dhe!m0X0Pk9BuRHdMg_8$}$k|GzvZ1Q)1M|2|{%2#% z%h5_zeFeDbsNXE`Jn(I^*?irxPb{W-K)qfsS1Oea;8B+Wt+mru|Jx7|*F|VpDwUo} zJ8DezfNHh6gyZBhCK!!IZ>Bxy)UB|pE<~!n1eUC|H{tLv7~>b6NZ5l!)FKX&Pt7W32!iB64JYauT^2pkA++Imjhs znOu1c_*_KpcI}Xx0dmYNJ#LJ7)wOYMYrwS3(s5}t8goGqbb#{_Sx#qyY2wX*4j^#$oK96 zs=7-2$pbtDtR|K3-2lGvZUAG=Nz&Lw5 zKfYhDbMOv8BGOt*+U@qa;c(cARBujfJb}#K8HyqzF99FtAxl-?==b{{y0p!0A5g2+ zt^yl5h`M2nx#HS5HxB?1PHnm}O$8$HpNyGcIfNu%|_*TF-{{o&LVW*`g#X|r9002ovPDHLkV1lci B6SDvS literal 0 HcmV?d00001 diff --git a/Packages/src/Editor/Icons/PasteIcon.png.meta b/Packages/src/Editor/Icons/PasteIcon.png.meta new file mode 100644 index 0000000..09ab446 --- /dev/null +++ b/Packages/src/Editor/Icons/PasteIcon.png.meta @@ -0,0 +1,109 @@ +fileFormatVersion: 2 +guid: 8c562a51524754926a36370d17d4b181 +TextureImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 12 + mipmaps: + mipMapMode: 0 + enableMipMap: 0 + sRGBTexture: 1 + linearTexture: 0 + fadeOut: 0 + borderMipMap: 0 + mipMapsPreserveCoverage: 0 + alphaTestReferenceValue: 0.5 + mipMapFadeDistanceStart: 1 + mipMapFadeDistanceEnd: 3 + bumpmap: + convertToNormalMap: 0 + externalNormalMap: 0 + heightScale: 0.25 + normalMapFilter: 0 + isReadable: 0 + streamingMipmaps: 0 + streamingMipmapsPriority: 0 + vTOnly: 0 + grayScaleToAlpha: 0 + generateCubemap: 6 + cubemapConvolution: 0 + seamlessCubemap: 0 + textureFormat: 1 + maxTextureSize: 2048 + textureSettings: + serializedVersion: 2 + filterMode: 1 + aniso: 1 + mipBias: 0 + wrapU: 1 + wrapV: 1 + wrapW: 0 + nPOTScale: 0 + lightmap: 0 + compressionQuality: 50 + spriteMode: 0 + spriteExtrude: 1 + spriteMeshType: 1 + alignment: 0 + spritePivot: {x: 0.5, y: 0.5} + spritePixelsToUnits: 100 + spriteBorder: {x: 0, y: 0, z: 0, w: 0} + spriteGenerateFallbackPhysicsShape: 1 + alphaUsage: 1 + alphaIsTransparency: 1 + spriteTessellationDetail: -1 + textureType: 2 + textureShape: 1 + singleChannelComponent: 0 + flipbookRows: 1 + flipbookColumns: 1 + maxTextureSizeSet: 0 + compressionQualitySet: 0 + textureFormatSet: 0 + ignorePngGamma: 0 + applyGammaDecoding: 0 + cookieLightType: 0 + platformSettings: + - serializedVersion: 3 + buildTarget: DefaultTexturePlatform + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + - serializedVersion: 3 + buildTarget: Standalone + maxTextureSize: 2048 + resizeAlgorithm: 0 + textureFormat: -1 + textureCompression: 1 + compressionQuality: 50 + crunchedCompression: 0 + allowsAlphaSplitting: 0 + overridden: 0 + androidETC2FallbackOverride: 0 + forceMaximumCompressionQuality_BC6H_BC7: 0 + spriteSheet: + serializedVersion: 2 + sprites: [] + outline: [] + physicsShape: [] + bones: [] + spriteID: + internalID: 0 + vertices: [] + indices: + edges: [] + weights: [] + secondaryTextures: [] + spritePackingTag: + pSDRemoveMatte: 0 + pSDShowRemoveMatteOption: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Editor/Icons/d_PasteIcon.png b/Packages/src/Editor/Icons/d_PasteIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..c6f6ff2e012b13799fa15e550767a8c38be7f97a GIT binary patch literal 522 zcmV+l0`>igP)g9;RDJK zwTv$Gk94s_d4bdZ(c`$6PqR9qQKQyTkKFZ|`BV?WHLTL8;9B(9uV<*o^$c_*Rmzge zwAx324)m|QlL0O0A!*@4Et4I70ngAw^MW-pU=#J2U0+= 0) + { + property = property.Substring(commentEndIndex + 2).Trim(); + } + + if (property.Equals("background", StringComparison.OrdinalIgnoreCase) + || property.Equals("background-color", StringComparison.OrdinalIgnoreCase)) + { + value = declaration.Substring(separatorIndex + 1).Trim(); + return true; + } + } + + value = null; + return false; + } + + private static bool TryParseRgbComponent(string text, out float value) + { + var isPercentage = text.EndsWith("%", StringComparison.Ordinal); + if (!float.TryParse(isPercentage ? text.Substring(0, text.Length - 1) : text, + NumberStyles.Float, CultureInfo.InvariantCulture, out value) + || float.IsNaN(value) || float.IsInfinity(value)) + { + return false; + } + + value = Mathf.Clamp01(isPercentage ? value / 100f : value / 255f); + return true; + } + + private static bool TryParseAlphaComponent(string text, out float value) + { + var isPercentage = text.EndsWith("%", StringComparison.Ordinal); + if (!float.TryParse(isPercentage ? text.Substring(0, text.Length - 1) : text, + NumberStyles.Float, CultureInfo.InvariantCulture, out value) + || float.IsNaN(value) || float.IsInfinity(value)) + { + return false; + } + + value = Mathf.Clamp01(isPercentage ? value / 100f : value); + return true; + } + } +} diff --git a/Packages/src/Editor/PasteColorUtil.cs.meta b/Packages/src/Editor/PasteColorUtil.cs.meta new file mode 100644 index 0000000..3caafea --- /dev/null +++ b/Packages/src/Editor/PasteColorUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e2fe72e593aed4b90a864e52a745a813 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Editor/PasteGradientUtil.cs b/Packages/src/Editor/PasteGradientUtil.cs new file mode 100644 index 0000000..3bbf0d3 --- /dev/null +++ b/Packages/src/Editor/PasteGradientUtil.cs @@ -0,0 +1,293 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using UnityEditor; +using UnityEngine; + +namespace Coffee.UIEffects.Editors +{ + internal static class PasteGradientUtil + { + private struct GradientStop + { + public Color color; + public float? position; + } + + internal static void PasteFromClipboard(SerializedProperty property, + Func getGradient, + Action setGradient) + { + if (!TryParseCssGradient(GUIUtility.systemCopyBuffer, out var value)) + { + EditorUtility.DisplayDialog("Paste Gradient", + "The clipboard does not contain a supported CSS linear-gradient(...) or radial-gradient(...) value.", + "OK"); + return; + } + + value.mode = getGradient(property).mode; + setGradient(property, value); + } + + private static bool TryParseCssGradient(string text, out Gradient gradient) + { + gradient = null; + if (string.IsNullOrWhiteSpace(text)) + { + return false; + } + + text = text.Trim(); + var linearIndex = text.IndexOf("linear-gradient(", StringComparison.OrdinalIgnoreCase); + var radialIndex = text.IndexOf("radial-gradient(", StringComparison.OrdinalIgnoreCase); + var functionIndex = linearIndex < 0 + ? radialIndex + : radialIndex < 0 + ? linearIndex + : Math.Min(linearIndex, radialIndex); + if (functionIndex < 0) + { + return false; + } + + text = text.Substring(functionIndex); + var openIndex = text.IndexOf('('); + if (openIndex < 0) + { + return false; + } + + var functionName = text.Substring(0, openIndex).Trim(); + var isRadial = functionName.Equals("radial-gradient", StringComparison.OrdinalIgnoreCase); + if (!isRadial && !functionName.Equals("linear-gradient", StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + var depth = 0; + var closeIndex = -1; + for (var i = openIndex; i < text.Length; i++) + { + if (text[i] == '(') + { + depth++; + } + else if (text[i] == ')') + { + depth--; + if (depth < 0) + { + return false; + } + + if (depth == 0) + { + closeIndex = i; + break; + } + } + } + + if (closeIndex < 0) + { + return false; + } + + var arguments = new List(); + if (!TrySplitGradientArguments(text.Substring(openIndex + 1, closeIndex - openIndex - 1), arguments) + || arguments.Count < 2) + { + return false; + } + + var firstStopIndex = 0; + if (!TryParseGradientStop(arguments[0], out _) + && (isRadial || IsGradientDirection(arguments[0]))) + { + firstStopIndex = 1; + } + + var stops = new List(); + for (var i = firstStopIndex; i < arguments.Count; i++) + { + if (!TryParseGradientStop(arguments[i], out var stop)) + { + return false; + } + + stops.Add(stop); + } + + if (stops.Count < 2) + { + return false; + } + + if (!stops[0].position.HasValue) + { + stops[0] = SetStopPosition(stops[0], 0); + } + + var lastStopIndex = stops.Count - 1; + if (!stops[lastStopIndex].position.HasValue) + { + stops[lastStopIndex] = SetStopPosition(stops[lastStopIndex], 1); + } + + var previousPositionIndex = 0; + for (var i = 1; i < stops.Count; i++) + { + if (!stops[i].position.HasValue) + { + continue; + } + + var previousPosition = stops[previousPositionIndex].position.Value; + var currentPosition = stops[i].position.Value; + var interval = i - previousPositionIndex; + for (var j = 1; j < interval; j++) + { + var position = Mathf.Lerp(previousPosition, currentPosition, (float)j / interval); + stops[previousPositionIndex + j] = + SetStopPosition(stops[previousPositionIndex + j], position); + } + + previousPositionIndex = i; + } + + var colorKeys = new GradientColorKey[stops.Count]; + var alphaKeys = new GradientAlphaKey[stops.Count]; + var previousTime = 0f; + for (var i = 0; i < stops.Count; i++) + { + var stop = stops[i]; + var time = Mathf.Clamp01(stop.position.Value); + time = Mathf.Max(previousTime, time); + colorKeys[i] = new GradientColorKey(new Color(stop.color.r, stop.color.g, stop.color.b), time); + alphaKeys[i] = new GradientAlphaKey(stop.color.a, time); + previousTime = time; + } + + gradient = new Gradient(); + gradient.SetKeys(colorKeys, alphaKeys); + return true; + } + + private static bool TrySplitGradientArguments(string text, List arguments) + { + var depth = 0; + var argumentStart = 0; + for (var i = 0; i < text.Length; i++) + { + if (text[i] == '(') + { + depth++; + } + else if (text[i] == ')') + { + depth--; + if (depth < 0) + { + return false; + } + } + else if (text[i] == ',' && depth == 0) + { + arguments.Add(text.Substring(argumentStart, i - argumentStart).Trim()); + argumentStart = i + 1; + } + } + + if (depth != 0) + { + return false; + } + + arguments.Add(text.Substring(argumentStart).Trim()); + return true; + } + + private static bool TryParseGradientStop(string text, out GradientStop stop) + { + stop = default; + text = text.Trim(); + + var colorLength = text.Length; + var openIndex = text.IndexOf('('); + if (openIndex > 0 + && (text.Substring(0, openIndex).Trim().Equals("rgb", StringComparison.OrdinalIgnoreCase) + || text.Substring(0, openIndex).Trim().Equals("rgba", StringComparison.OrdinalIgnoreCase))) + { + var closeIndex = text.IndexOf(')', openIndex + 1); + if (closeIndex < 0) + { + return false; + } + + colorLength = closeIndex + 1; + } + else + { + var whitespaceIndex = text.IndexOfAny(new[] { ' ', '\t', '\r', '\n' }); + if (whitespaceIndex >= 0) + { + colorLength = whitespaceIndex; + } + } + + var colorText = text.Substring(0, colorLength).Trim(); + var positionText = text.Substring(colorLength).Trim(); + if (!PasteColorUtil.TryParseColor(colorText, out var color)) + { + return false; + } + + float? position = null; + if (positionText.Length > 0) + { + if (!positionText.EndsWith("%", StringComparison.Ordinal) + || !float.TryParse(positionText.Substring(0, positionText.Length - 1), + NumberStyles.Float, CultureInfo.InvariantCulture, out var percentage) + || float.IsNaN(percentage) || float.IsInfinity(percentage)) + { + return false; + } + + position = percentage / 100f; + } + + stop.color = color; + stop.position = position; + return true; + } + + private static bool IsGradientDirection(string text) + { + text = text.Trim(); + if (text.StartsWith("to ", StringComparison.OrdinalIgnoreCase)) + { + return true; + } + + foreach (var suffix in new[] { "deg", "grad", "rad", "turn" }) + { + if (text.EndsWith(suffix, StringComparison.OrdinalIgnoreCase) + && float.TryParse(text.Substring(0, text.Length - suffix.Length), + NumberStyles.Float, CultureInfo.InvariantCulture, out var direction) + && !float.IsNaN(direction) && !float.IsInfinity(direction)) + { + return true; + } + } + + return false; + } + + private static GradientStop SetStopPosition(GradientStop stop, float position) + { + stop.position = position; + return stop; + } + } +} diff --git a/Packages/src/Editor/PasteGradientUtil.cs.meta b/Packages/src/Editor/PasteGradientUtil.cs.meta new file mode 100644 index 0000000..7468408 --- /dev/null +++ b/Packages/src/Editor/PasteGradientUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 271608aaaedb44ac39b1357557a71342 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Editor/UIEffectPropertyEditor.cs b/Packages/src/Editor/UIEffectPropertyEditor.cs index 8c05644..2d8606f 100644 --- a/Packages/src/Editor/UIEffectPropertyEditor.cs +++ b/Packages/src/Editor/UIEffectPropertyEditor.cs @@ -1,4 +1,4 @@ -using UnityEditor; +using UnityEditor; using UnityEngine; using System; using System.Reflection; @@ -20,6 +20,10 @@ namespace Coffee.UIEffects.Editors s_PiGradient.SetMethod); private static GUIContent s_ReverseGradientContent; + private static GUIContent s_PasteColorContent; + private static GUIContent s_PasteGradientContent; + private static readonly GUIContent s_RotationLabel = new GUIContent("* Rotation"); + private static readonly GUIContent s_KeepAspectRatioLabel = new GUIContent("* Keep Aspect Ratio"); private SerializedProperty _toneFilter; private SerializedProperty _toneIntensity; @@ -107,8 +111,6 @@ namespace Coffee.UIEffects.Editors private SerializedProperty _detailTexSpeed; private SerializedProperty _flip; - private static GUIContent s_RotationLabel = new GUIContent("* Rotation"); - private static GUIContent s_KeepAspectRatioLabel = new GUIContent("* Keep Aspect Ratio"); protected virtual void OnEnable() { @@ -116,6 +118,10 @@ namespace Coffee.UIEffects.Editors { var iconDir = "Packages/com.coffee.ui-effect/Editor/Icons"; s_ReverseGradientContent = Misc.GetIconContent($"{iconDir}/ReverseIcon.png", "Reverse gradient"); + s_PasteColorContent = Misc.GetIconContent($"{iconDir}/PasteIcon.png", + $"Paste {PasteColorUtil.SupportedFormats} or CSS background/background-color declarations."); + s_PasteGradientContent = Misc.GetIconContent($"{iconDir}/PasteIcon.png", + "Paste CSS linear-gradient(...) or radial-gradient(...)."); } _toneFilter = serializedObject.FindProperty("m_ToneFilter"); @@ -511,20 +517,31 @@ namespace Coffee.UIEffects.Editors var hdr = UIEffectProjectSettings.useHdrColorPicker; EditorGUI.showMixedValue = color.hasMultipleDifferentValues; + const float pasteWidth = 20f; + var colorRect = new Rect(rect.x, rect.y, rect.width - pasteWidth - 2, rect.height); + var pasteRect = new Rect(colorRect.xMax + 2, rect.y, pasteWidth, rect.height); + EditorGUI.BeginChangeCheck(); - var colorValue = EditorGUI.ColorField(rect, label, color.colorValue, true, showAlpha, hdr); + var colorValue = EditorGUI.ColorField(colorRect, label, color.colorValue, true, showAlpha, hdr); if (EditorGUI.EndChangeCheck()) { color.colorValue = colorValue; } + + if (GUI.Button(pasteRect, s_PasteColorContent, "IconButton")) + { + PasteColorUtil.PasteFromClipboard(color); + } } private static void DrawGradientField(SerializedProperty gradient) { var r = EditorGUILayout.GetControlRect(); const float reverseWidth = 20f; - var gradientRect = new Rect(r.x, r.y, r.width - reverseWidth - 2, r.height); + const float pasteWidth = 20f; + var gradientRect = new Rect(r.x, r.y, r.width - reverseWidth - pasteWidth - 4, r.height); var reverseRect = new Rect(gradientRect.xMax + 2, r.y, reverseWidth, r.height); + var pasteRect = new Rect(reverseRect.xMax + 2, r.y, pasteWidth, r.height); var label = EditorGUIUtility.TrTempContent(gradient.displayName); label.tooltip = gradient.tooltip; var hdr = UIEffectProjectSettings.useHdrColorPicker; @@ -541,6 +558,11 @@ namespace Coffee.UIEffects.Editors { ReverseGradient(gradient); } + + if (GUI.Button(pasteRect, s_PasteGradientContent, "IconButton")) + { + PasteGradientUtil.PasteFromClipboard(gradient, s_GetGradient, s_SetGradient); + } } private static void ReverseGradient(SerializedProperty property)