Ejemplo n.º 1
0
        /// <summary>
        ///     Move the cross hair to the new saturation and brightness
        /// </summary>
        ///
        /// <param name="hsbColor">
        ///     New color in HSB
        /// </param>
        public void MoveToColor(HSBColor hsbColor)
        {
            if (hsbSwatch == null)
            {
                Start();
            }

            float minX = hsbSwatch.transform.position.x - (hsbSwatch.GetComponent <RectTransform>().rect.width / 2);
            float maxX = minX + hsbSwatch.GetComponent <RectTransform>().rect.width;

            float minY = hsbSwatch.transform.position.y - hsbSwatch.GetComponent <RectTransform>().rect.height + (hsbSwatch.GetComponent <RectTransform>().rect.height / 2);
            float maxY = hsbSwatch.transform.position.y + (hsbSwatch.GetComponent <RectTransform>().rect.height / 2);

            float rangeX = maxY - minY;
            float rangeY = maxY - minY;

            float newX = minX + (hsbColor.s * rangeX);
            float newY = minY + (hsbColor.b * rangeY);

            HSBColor huedColor = new HSBColor(hsbColor.h, 1.0f, 1.0f);

            hsbSwatch.GetComponent <Image>().material.SetColor("_Color", huedColor.ToColor());

            transform.position = new Vector2(newX, newY);
        }
Ejemplo n.º 2
0
        //Generates a 256x256 texture with all variations for the selected HUE
        void CreateHSBTexture(HSBColor color)
        {
            int       size    = 256;
            Texture2D texture = new Texture2D(size, size, TextureFormat.ARGB32, false);

            texture.filterMode = FilterMode.Point;

            Color[] textureData = new Color[size * size];
            color.s = 0;
            color.b = 1;

            for (int x = 0; x < size; x++)
            {
                for (int y = 0; y < size; y++)
                {
                    color.s = Mathf.Clamp(x / (float)(size - 1), 0, 1);
                    color.b = Mathf.Clamp(y / (float)(size - 1), 0, 1);
                    textureData[x + y * size] = color.ToColor();
                }
            }

            texture.SetPixels(textureData);
            texture.Apply();

            SpriteRenderer.sprite = Sprite.Create(texture, new Rect(0, 0, size, size), new Vector2(0f, 1f), 233);
            Data = textureData;
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     On drag event, update the current hue
        /// </summary>
        public void OnDrag()
        {
            // Clamp the Y value so it remains in the parent huePanel
            float minY = huePanel.transform.position.y - huePanel.GetComponent <RectTransform>().rect.height + (huePanel.GetComponent <RectTransform>().rect.height / 2) + 3;
            float maxY = huePanel.transform.position.y + (huePanel.GetComponent <RectTransform>().rect.height / 2);

            Vector2 pos;

            RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out pos);

            float newY = canvas.transform.TransformPoint(pos).y;

            newY = Mathf.Clamp(newY, minY, maxY);
            transform.position = new Vector2(transform.position.x, newY);

            // Work out the new hue based on the Y position of the slider
            float range = maxY - minY;
            float diff  = newY - minY;
            float hue   = diff / range;

            // Push the new hue to the ColorPicker script on the
            colorChooser.GetComponent <ColorPicker>().SetColor(hue, -1.0f, -1.0f);

            // Push the new color to the HSBSwatch?
            HSBColor huedColor = new HSBColor(hue, 1.0f, 1.0f);

            hsbSwatch.material.SetColor("_Color", huedColor.ToColor());
        }
Ejemplo n.º 4
0
        private void UpdateColor(HSBColor color)
        {
            Color = color.ToColor();
            Hex   = ColorToHex(Color);

            H = color.h;
            S = color.s;
            B = color.b;
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Store the current color choosing, remembering the previous color
        /// </summary>
        public void OK()
        {
            previousColor            = currentColor;
            previousColorImage.color = previousColor.ToColor();

            for (int i = (recentColorImages.Length - 1); i >= 0; i--)
            {
                if (i == 0)
                {
                    recentColorImages[i].GetComponent <Image>().color = previousColor.ToColor();
                }
                else
                {
                    Color beforeColor = recentColorImages[i - 1].GetComponent <Image>().color;
                    recentColorImages[i].GetComponent <Image>().color = beforeColor;
                }
            }

            gameObject.SetActive(false);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Update the current color based on changes from a different UI component
        /// </summary>
        ///
        /// <param name="hue">
        ///     New hue value (if less than 0.0, assume hue unchanged)
        /// </param>
        /// <param name="saturation">
        ///     New saturation value (if less than 0.0, assume saturation unchanged)
        /// </param>
        /// <param name="brightness">
        ///     New brightness value (if less than 0.0, assume brightness unchanged)
        /// </param>
        public void SetColor(float hue, float saturation, float brightness)
        {
            // Updated HSB values
            float newHue        = currentColor.h;
            float newSaturation = currentColor.s;
            float newBrightness = currentColor.b;

            // Update the HUE
            if (hue >= 0.0f)
            {
                newHue = hue;
            }
            // Update the saturation
            if (saturation > 0.0f)
            {
                newSaturation = saturation;
            }
            // Update the brightness
            if (brightness > 0.0f)
            {
                newBrightness = brightness;
            }

            currentColor = new HSBColor(newHue, newSaturation, newBrightness);
            Color rgbColor = currentColor.ToColor();

            // Update the UI components with the new color's values
            currentColorImage.color = rgbColor;
            hexInputField.text      = "#" + ColorToHex(rgbColor);

            float r = (float)Math.Truncate(rgbColor.r * 1000.0f) / 1000.0f;
            float g = (float)Math.Truncate(rgbColor.g * 1000.0f) / 1000.0f;
            float b = (float)Math.Truncate(rgbColor.b * 1000.0f) / 1000.0f;

            // Update the RGB Input Fields
            redInputField.text   = ((int)(r * 255)).ToString();
            greenInputField.text = ((int)(g * 255)).ToString();
            blueInputField.text  = ((int)(b * 255)).ToString();

            if (saturation < 0.0f || brightness < 0.0f)
            {
                // Update the shader on the HSB swatch
                hsbSwatch.MoveToColor(new HSBColor(rgbColor));
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     On wake up, find various UI components and reset the HSB shader to match the initially selected color
        /// </summary>
        public void Start()
        {
            // Find UI components
            currentColorImage  = gameObject.transform.Find("currentColor").GetComponent <Image>();
            previousColorImage = gameObject.transform.Find("previousColor").GetComponent <Image>();
            redInputField      = gameObject.transform.Find("redControls/redInputField").GetComponent <InputField>();
            greenInputField    = gameObject.transform.Find("greenControls/greenInputField").GetComponent <InputField>();
            blueInputField     = gameObject.transform.Find("blueControls/blueInputField").GetComponent <InputField>();
            hexInputField      = gameObject.transform.Find("hexInputField").GetComponent <InputField>();

            // Find script instances
            hueSlider = gameObject.transform.Find("huePanel/slider").GetComponent <HueSlider>();
            hsbSwatch = gameObject.transform.Find("hsbSwatch/crosshair").GetComponent <HSBSwatch>();

            // Fire an initial event to update the HSB shader
            RGBChanged();

            recentColorImages[0].GetComponent <Image>().color = previousColor.ToColor();
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Revert to the input image's color
        /// </summary>
        public void RevertToColor(Image image)
        {
            currentColor = new HSBColor(image.color);
            Color currentColorRGB = currentColor.ToColor();

            // Update the current color image
            currentColorImage.color = currentColorRGB;

            // Update the RGB Input Fields
            redInputField.text   = ((int)(currentColorRGB.r * 255)).ToString();
            greenInputField.text = ((int)(currentColorRGB.g * 255)).ToString();
            blueInputField.text  = ((int)(currentColorRGB.b * 255)).ToString();

            // Update the hexInputField
            hexInputField.text = "#" + ColorToHex(currentColorRGB);

            // Update the position of the Hue slider
            hueSlider.MoveToColor(currentColorRGB);

            // Update the shader on the HSB swatch
            hsbSwatch.MoveToColor(new HSBColor(currentColorRGB));
        }