UIColorPicker - jimdroberts/FishMMO GitHub Wiki
UI control for picking colors using HSV and RGB sliders, and displaying the color in various formats. Provides a comprehensive interface for color selection and conversion, supporting both user interaction and programmatic access. Used in FishMMO client UI for color customization.
-
public const int SLIDER_BACKGROUND_WIDTH
Width of slider backgrounds in pixels.
-
public const int SLIDER_BACKGROUND_HEIGHT
Height of slider backgrounds in pixels.
-
public const int HSV_TEXTURE_WIDTH
Width of HSV texture in pixels.
-
public const int HSV_TEXTURE_HEIGHT
Height of HSV texture in pixels.
-
public const int NUM_HUES
Number of hue values supported (degrees).
-
public const int HUE_MAX
Maximum hue value (359).
-
public const int SV_MAX
Maximum saturation/value (100).
-
public const int RGBA_MAX
Maximum RGBA value (255).
-
public const int MAX_NEW_PALETTES
Maximum number of new palettes supported.
-
public Color InitialColor
Initial color for the picker.
-
public Sprite[] CachedHSVSprites
Cached HSV sprites for each hue value.
-
public Image Current
Image displaying the currently selected color.
-
public Image Cursor
Image representing the cursor position in the HSV texture.
-
public RageInputField HexInputField
Input field for hexadecimal color value.
-
public Image HTexture, HBackground; public RageSlider HSlider; public RageInputField HInputField
UI elements for hue selection.
-
public Image SBackground; public RageSlider SSlider; public RageInputField SInputField
UI elements for saturation selection.
-
public Image VBackground; public RageSlider VSlider; public RageInputField VInputField
UI elements for value (brightness) selection.
-
public Image RBackground; public RageSlider RSlider; public RageInputField RInputField
UI elements for red channel.
-
public Image GBackground; public RageSlider GSlider; public RageInputField GInputField
UI elements for green channel.
-
public Image BBackground; public RageSlider BSlider; public RageInputField BInputField
UI elements for blue channel.
-
public Image ABackground; public RageSlider ASlider; public RageInputField AInputField
UI elements for alpha (transparency) channel.
-
public override void OnStarting()
Cache HSV sprites and set the initial color.
-
public void SetColor(Color color)
Set the color of the picker and update all related UI elements. Parameters: Color color – The color to set.
-
public void PickTexture(BaseEventData baseData)
Pick a color from the texture based on the cursor's position and update the color picker. Parameters: BaseEventData baseData – Event data containing the pointer's position.
-
public void SetCursor()
Set the cursor's position based on the current color's HSV values.
-
public void UpdateHexValue(string value)
Update the color picker value from a hexadecimal string. Parameters: string value – The hexadecimal color string.
-
public void UpdateHueSliderValue(float value)
Update the hue slider and related values when the hue is changed. Parameters: float value – The new hue value.
-
public void UpdateHueInputValue(string value)
Update the hue value from an input field and refresh the UI. Parameters: string value – The hue value as a string.
-
public void UpdateSaturationSliderValue(float value)
Update the saturation value and refresh the color display. Parameters: float value – The new saturation value.
-
public void UpdateSaturationInputValue(string value)
Update the saturation value from an input field and refresh the UI. Parameters: string value – The saturation value as a string.
-
public void UpdateValueSliderValue(float value)
Update the value (brightness) slider and refresh the color display. Parameters: float value – The new value (brightness) value.
-
public void UpdateValueInputValue(string value)
Update the value (brightness) from an input field and refresh the UI. Parameters: string value – The value (brightness) as a string.
-
public void UpdateRedSliderValue(float value)
Update the red color channel slider and refresh the color display. Parameters: float value – The new red value.
-
public void UpdateRedInputValue(string value)
Update the red color channel value from an input field and refresh the UI. Parameters: string value – The red value as a string.
-
public void UpdateGreenSliderValue(float value)
Update the green color channel slider and refresh the color display. Parameters: float value – The new green value.
-
public void UpdateGreenInputValue(string value)
Update the green color channel value from an input field and refresh the UI. Parameters: string value – The green value as a string.
-
public void UpdateBlueSliderValue(float value)
Update the blue color channel slider and refresh the color display. Parameters: float value – The new blue value.
-
public void UpdateBlueInputValue(string value)
Update the blue color channel value from an input field and refresh the UI. Parameters: string value – The blue value as a string.
-
public void UpdateAlphaSliderValue(float value)
Update the alpha (transparency) slider and refresh the color display. Parameters: float value – The new alpha value.
-
public void UpdateAlphaInputValue(string value)
Update the alpha (transparency) value from an input field and refresh the UI. Parameters: string value – The alpha value as a string.
- Ensure all required UI elements (sliders, input fields, images) are assigned in the Unity Inspector.
- Attach the
UIColorPicker
script to a UI GameObject. - Configure the
InitialColor
and other settings as needed. - Call
SetColor()
to initialize the picker with a specific color if desired.
// Example: Setting up and using UIColorPicker in a UI panel
// 1. Reference the UIColorPicker component from your script or inspector.
// 2. Set the initial color or update color based on user input.
public class ColorPickerExample : MonoBehaviour
{
public UIColorPicker colorPicker;
void Start()
{
// Set the picker to a specific color at startup
colorPicker.SetColor(Color.green);
}
public void OnUserColorSelected(Color newColor)
{
// Update picker and UI with the new color
colorPicker.SetColor(newColor);
}
}
- Always assign all required UI references in the Inspector to avoid null reference errors.
- Use the provided methods to update color values; do not manipulate UI elements directly.
- Cache HSV sprites for performance if using many color pickers.
- Validate user input for color fields to prevent invalid color values.
- Integrate with other UI systems (e.g., palettes, presets) for a better user experience.