public Color Evaluate(float time) { SortKeys(); GradientKey lastKey = keys[keys.Count - 1]; //If the time is over the time of the last key we return the last key color. if (time > lastKey.time) { return(lastKey.color); } for (int i = 0; i < keys.Count - 1; i++) { GradientKey actualKey = keys[i]; GradientKey nextKey = keys[i + 1]; if (time >= actualKey.time && time <= keys[i + 1].time) { return(Color.Lerp(actualKey.color, nextKey.color, (time - actualKey.time) / (nextKey.time - actualKey.time))); } } return(keys[0].color); }
public void Initialize(Gradient gradient, Action <Gradient> onAffirmatedCallback) { foreach (GradientAlphaKey gradientAlphaKey in gradient.alphaKeys) { Vector2 parentSize = _alphaKeysParent.rect.size; Vector2 localPos = Vector2.zero; localPos.x = (gradientAlphaKey.time - _alphaKeysParent.pivot.x) * parentSize.x + (parentSize.x / 2); GradientKey alphaKey = InstantiateGradientKey(localPos, _alphaKeysParent, true, gradientAlphaKey.time); alphaKey.SetAlpha(gradientAlphaKey.alpha); _alphasKeys.Add(alphaKey); } foreach (GradientColorKey gradientColorKey in gradient.colorKeys) { Vector2 parentSize = _colorKeysParent.rect.size; Vector2 localPos = Vector2.zero; localPos.x = (gradientColorKey.time - _colorKeysParent.pivot.x) * parentSize.x + (parentSize.x / 2); GradientKey colorKey = InstantiateGradientKey(localPos, _colorKeysParent, false, gradientColorKey.time); colorKey.SetColor(gradientColorKey.color); _colorsKeys.Add(colorKey); } _onAffirmativeCallback = onAffirmatedCallback; _gradient = gradient; CreateTexture(); }
private GradientKey InstantiateGradientKey(Vector2 position, RectTransform parent, bool isAlpha, float time) { GradientKey gradientKey = (Instantiate(_gradientKeyItem, Vector3.zero, Quaternion.identity, parent)); RectTransform rt = gradientKey.GetComponent <RectTransform>(); rt.anchoredPosition = position; rt.SetParent(parent); gradientKey.Initialize(Guid.NewGuid().ToString("N"), isAlpha, time, _flexibleColorPicker, OnKeySelected, OnKeyDeleted, UpdateGradient); return(gradientKey); }
public void CreateColorKey(BaseEventData e) { if (_colorsKeys.Count < 8) { Vector2 v = FlexibleColorPicker.GetNormalizedPointerPosition(_colorKeysParent, e); Vector2 parentSize = _colorKeysParent.rect.size; Vector2 localPos = Vector2.zero; localPos.x = (v.x - _colorKeysParent.pivot.x) * parentSize.x + (parentSize.x / 2); GradientKey gradientKey = InstantiateGradientKey(localPos, _colorKeysParent, false, v.x); _colorsKeys.Add(gradientKey); UpdateGradient(); } }
private void OnKeySelected(GradientKey selectedKey) { _alphasKeys.ForEach(t => { if (t.Guid != selectedKey.Guid) { t.Deselect(); } }); _colorsKeys.ForEach(t => { if (t.Guid != selectedKey.Guid) { t.Deselect(); } }); }
public Gradient(float[] positions, T[] values) { if (LerpFunc == null) { throw new NotSupportedException($"Gradient<{typeof(T).Name}>.lerpFunc is not defined."); } if (positions.Length != values.Length || positions.Length == 0) { throw new ArgumentException("Array lengths must be equal and not be zero."); } keys = new GradientKey[positions.Length]; for (int i = 0; i < keys.Length; i++) { keys[i] = new GradientKey(positions[i], values[i]); } }
private void UpdateGradient() { Texture2D tex = (Texture2D)_rawImage.texture; int width = tex.width; int height = tex.height; Color[] pixels = new Color[width * height]; GradientAlphaKey[] gradientAlphaKeys = new GradientAlphaKey[_alphasKeys.Count]; GradientColorKey[] gradientColorKeys = new GradientColorKey[_colorsKeys.Count]; for (int i = 0; i < _alphasKeys.Count; i++) { GradientKey alphaKey = _alphasKeys[i]; gradientAlphaKeys[i].alpha = alphaKey.Alpha; gradientAlphaKeys[i].time = alphaKey.Time; } for (int i = 0; i < _colorsKeys.Count; i++) { GradientKey colorKey = _colorsKeys[i]; gradientColorKeys[i].color = colorKey.Color; gradientColorKeys[i].time = colorKey.Time; } _gradient.alphaKeys = gradientAlphaKeys; _gradient.colorKeys = gradientColorKeys; for (int x = 0; x < width; x++) { float normX = (float)x / (width - 1); for (int y = 0; y < height; y++) { Color color = _gradient.Evaluate(normX); int pixelIndex = x + y * width; pixels[pixelIndex] = color; } } tex.SetPixels(pixels); tex.Apply(); }
private void OnKeyDeleted(GradientKey deletedKey) { if (deletedKey.IsAlpha) { if (_alphasKeys.Count > 1) { _alphasKeys.RemoveAll(t => t.Guid == deletedKey.Guid); Destroy(deletedKey.gameObject); UpdateGradient(); } } else { if (_colorsKeys.Count > 1) { _colorsKeys.RemoveAll(t => t.Guid == deletedKey.Guid); Destroy(deletedKey.gameObject); UpdateGradient(); } } }
public T GetValue(float time) { GradientKey left = null; GradientKey right = null; for (int i = 0; i < keys.Length; i++) { if (left == null || keys[i].time > left.time && keys[i].time <= time) { left = keys[i]; } } for (int i = keys.Length - 1; i >= 0; i--) { if (right == null || keys[i].time < right.time && keys[i].time >= time) { right = keys[i]; } } return(left.time == right.time ? left.value : LerpFunc(left.value, right.value, (time - left.time) / (right.time - left.time))); }