@ -13,16 +13,7 @@ namespace Connected.Components;
public partial class ColorPicker : Picker < Color > , IAsyncDisposable
public partial class ColorPicker : Picker < Color > , IAsyncDisposable
{
{
public ColorPicker ( ) : base ( new DefaultConverter < Color > ( ) )
#region Variables
{
AdornmentIcon = Icons . Material . Outlined . Palette ;
DisableToolbar = true ;
Value = "#594ae2" ; //MudBlazor Blue
Text = GetColorTextValue ( ) ;
AdornmentAriaLabel = "Open Color Picker" ;
}
#region Fields
private static Dictionary < int , ( Func < int , int > r , Func < int , int > g , Func < int , int > b , string dominantColorPart ) > _rgbToHueMapper = new ( )
private static Dictionary < int , ( Func < int , int > r , Func < int , int > g , Func < int , int > b , string dominantColorPart ) > _rgbToHueMapper = new ( )
{
{
@ -50,225 +41,175 @@ public partial class ColorPicker : Picker<Color>, IAsyncDisposable
private readonly Guid _id = Guid . NewGuid ( ) ;
private readonly Guid _id = Guid . NewGuid ( ) ;
private Guid _throttledMouseOverEventId ;
private Guid _throttledMouseOverEventId ;
private IEventListener _throttledEventManager ;
private ColorPickerView _colorPickerView = ColorPickerView . Spectrum ;
[Inject] IEventListenerFactory ThrottledEventManagerFactory { get ; set ; }
private ColorPickerView _activeColorPickerView = ColorPickerView . Spectrum ;
# endregion
#region Parameters
[CascadingParameter(Name = "RightToLeft")] public bool RightToLeft { get ; set ; }
private bool _disableAlpha = false ;
private bool _disableAlpha = false ;
private IEventListener _throttledEventManager ;
/// <summary>
/// <summary>
/// If true, Alpha options will not be displayed and color output will be RGB, HSL or HEX and not RGBA, HSLA or HEXA.
/// MudColor list of predefined colors. The first five colors will show up as the quick colors on preview dot click .
/// </summary>
/// </summary>
[Parameter]
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableAlpha
public IEnumerable < Color > Palette { get ; set ; } = new Color [ ]
{
{ "#424242" , "#2196f3" , "#00c853" , "#ff9800" , "#f44336" ,
get = > _disableAlpha ;
"#f6f9fb" , "#9df1fa" , "#bdffcf" , "#fff0a3" , "#ffd254" ,
set
"#e6e9eb" , "#27dbf5" , "#7ef7a0" , "#ffe273" , "#ffb31f" ,
{
"#c9cccf" , "#13b8e8" , "#14dc71" , "#fdd22f" , "#ff9102" ,
if ( value ! = _disableAlpha )
"#858791" , "#0989c2" , "#1bbd66" , "#ebb323" , "#fe6800" ,
{
"#585b62" , "#17698e" , "#17a258" , "#d9980d" , "#dc3f11" ,
_disableAlpha = value ;
"#353940" , "#113b53" , "#127942" , "#bf7d11" , "#aa0000"
} ;
if ( value = = true )
private IEnumerable < Color > _gridList = new Color [ ]
{
{
Value = Value . SetAlpha ( 1.0 ) ;
"#FFFFFF" , "#ebebeb" , "#d6d6d6" , "#c2c2c2" , "#adadad" , "#999999" , "#858586" , "#707070" , "#5c5c5c" , "#474747" , "#333333" , "#000000" ,
}
"#133648" , "#071d53" , "#0f0638" , "#2a093b" , "#370c1b" , "#541107" , "#532009" , "#53350d" , "#523e0f" , "#65611b" , "#505518" , "#2b3d16" ,
"#1e4c63" , "#0f2e76" , "#180b4e" , "#3f1256" , "#4e1629" , "#781e0e" , "#722f10" , "#734c16" , "#73591a" , "#8c8629" , "#707625" , "#3f5623" ,
"#2e6c8c" , "#1841a3" , "#280c72" , "#591e77" , "#6f223d" , "#a62c17" , "#a0451a" , "#a06b23" , "#9f7d28" , "#c3bc3c" , "#9da436" , "#587934" ,
"#3c8ab0" , "#2155ce" , "#331c8e" , "#702898" , "#8d2e4f" , "#d03a20" , "#ca5a24" , "#c8862e" , "#c99f35" , "#f3ec4e" , "#c6d047" , "#729b44" ,
"#479fd3" , "#2660f5" , "#4725ab" , "#8c33b5" , "#aa395d" , "#eb512e" , "#ed732e" , "#f3ae3d" , "#f5c944" , "#fefb67" , "#ddeb5c" , "#86b953" ,
"#59c4f7" , "#4e85f6" , "#5733e2" , "#af43eb" , "#d44a7a" , "#ed6c59" , "#ef8c56" , "#f3b757" , "#f6cd5b" , "#fef881" , "#e6ee7a" , "#a3d16e" ,
"#78d3f8" , "#7fa6f8" , "#7e52f5" , "#c45ff6" , "#de789d" , "#f09286" , "#f2a984" , "#f6c983" , "#f9da85" , "#fef9a1" , "#ebf29b" , "#badc94" ,
"#a5e1fa" , "#adc5fa" , "#ab8df7" , "#d696f8" , "#e8a7bf" , "#f4b8b1" , "#f6c7af" , "#f9daae" , "#fae5af" , "#fefbc0" , "#f3f7be" , "#d2e7ba" ,
"#d2effd" , "#d6e1fc" , "#d6c9fa" , "#e9cbfb" , "#f3d4df" , "#f9dcd9" , "#fae3d8" , "#fcecd7" , "#fdf2d8" , "#fefce0" , "#f7fade" , "#e3edd6"
} ;
Text = GetColorTextValue ( ) ;
[Inject] IEventListenerFactory ThrottledEventManagerFactory { get ; set ; }
}
}
# endregion
}
#region Events
private EventCallback < MouseEventArgs > GetEventCallback ( ) = > EventCallback . Factory . Create < MouseEventArgs > ( this , ( ) = > Close ( ) ) ;
private EventCallback < MouseEventArgs > GetSelectPaletteColorCallback ( Color color ) = > new EventCallbackFactory ( ) . Create ( this , ( MouseEventArgs e ) = > SelectPaletteColor ( color ) ) ;
/// <summary>
/// <summary>
/// If true, the color field will not be displayed.
/// Set the R (red) component of the color picker
/// </summary>
/// </summary>
[Parameter]
/// <param name="value">A value between 0 (no red) or 255 (max red)</param>
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public void SetR ( int value ) = > Value = Value . SetR ( value ) ;
public bool DisableColorField { get ; set ; } = false ;
/// <summary>
/// <summary>
/// If true, the switch to change color mode will not be displayed.
/// Set the G (green) component of the color picker
/// </summary>
/// </summary>
[Parameter]
/// <param name="value">A value between 0 (no green) or 255 (max green)</param>
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public void SetG ( int value ) = > Value = Value . SetG ( value ) ;
public bool DisableModeSwitch { get ; set ; } = false ;
/// <summary>
/// <summary>
/// If true, textfield inputs and color mode switch will not be displayed.
/// Set the B (blue) component of the color picker
/// </summary>
/// </summary>
[Parameter]
/// <param name="value">A value between 0 (no blue) or 255 (max blue)</param>
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public void SetB ( int value ) = > Value = Value . SetB ( value ) ;
public bool DisableInputs { get ; set ; } = false ;
/// <summary>
/// <summary>
/// If true, hue and alpha sliders will not be displayed.
/// Set the H (hue) component of the color picker
/// </summary>
/// </summary>
[Parameter]
/// <param name="value">A value between 0 and 360 (degrees)</param>
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public void SetH ( double value ) = > Value = Value . SetH ( value ) ;
public bool DisableSliders { get ; set ; } = false ;
/// <summary>
/// <summary>
/// If true, the preview color box will not be displayed, note that the preview color functions as a button as well for collection colors.
/// Set the S (saturation) component of the color picker
/// </summary>
/// </summary>
[Parameter]
/// <param name="value">A value between 0.0 (no saturation) and 1.0 (max saturation)</param>
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public void SetS ( double value ) = > Value = Value . SetS ( value ) ;
public bool DisablePreview { get ; set ; } = false ;
/// <summary>
/// <summary>
/// The initial mode (RGB, HSL or HEX) the picker should open. Defaults to RGB
/// Set the L (Lightness) component of the color picker
/// </summary>
/// </summary>
[Parameter]
/// <param name="value">A value between 0.0 (no light, black) and 1.0 (max light, white)</param>
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public void SetL ( double value ) = > Value = Value . SetL ( value ) ;
public ColorPickerMode ColorPickerMode { get ; set ; } = ColorPickerMode . RGB ;
private ColorPickerView _colorPickerView = ColorPickerView . Spectrum ;
private ColorPickerView _activeColorPickerView = ColorPickerView . Spectrum ;
/// <summary>
/// <summary>
/// The initial view of the picker. Views can be changed if toolbar is enabled.
/// Set the Alpha (transparency) component of the color picker
/// </summary>
/// </summary>
[Parameter]
/// <param name="value">A value between 0.0 (full transparent) and 1.0 (solid) </param>
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public void SetAlpha ( double value ) = > Value = Value . SetAlpha ( value ) ;
public ColorPickerView ColorPickerView
{
get = > _colorPickerView ;
set
{
if ( value ! = _colorPickerView )
{
_colorPickerView = value ;
ChangeView ( value ) . AndForget ( ) ;
}
}
}
/// <summary>
/// <summary>
/// If true, binding changes occurred also when HSL values changed without a corresponding RGB change
/// Set the Alpha (transparency) component of the color picker
/// </summary>
/// </summary>
[Parameter]
/// <param name="value">A value between 0 (full transparent) and 1 (solid) </param>
[Category(CategoryTypes.FormComponent.Behavior)]
public void SetAlpha ( int value ) = > Value = Value . SetAlpha ( value ) ;
public bool UpdateBindingIfOnlyHSLChanged { get ; set ; } = false ;
/// <summary>
/// <summary>
/// A two-way bindable property representing the selected value. MudColor is a utility class that can be used to get the value as RGB, HSL, hex or other value
/// Set the color of the picker based on the string input
/// </summary>
/// </summary>
[Parameter]
/// <param name="input">Accepting different formats for a color representation such as rbg, rgba, #</param>
[Category(CategoryTypes.FormComponent.Data)]
public void SetInputString ( string input )
public Color Value
{
get = > _color ;
set
{
{
if ( value = = null ) { return ; }
Color color ;
try
var rgbChanged = value ! = _color ;
var hslChanged = _color = = null ? false : value . HslChanged ( _color ) ;
_color = value ;
if ( rgbChanged )
{
{
if ( _skipFeedback = = false )
color = new Color ( input ) ;
}
catch ( Exception )
{
{
UpdateBaseColor ( ) ;
return ;
UpdateColorSelectorBasedOnRgb ( ) ;
}
}
SetTextAsync ( GetColorTextValue ( ) , false ) . AndForget ( ) ;
Value = color ;
ValueChanged . InvokeAsync ( value ) . AndForget ( ) ;
FieldChanged ( value ) ;
}
}
if ( rgbChanged = = false & & UpdateBindingIfOnlyHSLChanged & & hslChanged = = tr ue)
protected override Task StringValueChanged ( string val ue)
{
{
SetTextAsync ( GetColorTextValue ( ) , false ) . AndForget ( ) ;
SetInputString ( value ) ;
ValueChanged . InvokeAsync ( value ) . AndForget ( ) ;
return Task . CompletedTask ;
FieldChanged ( value ) ;
}
}
}
}
[Parameter] public EventCallback < Color > ValueChanged { get ; set ; }
private bool _attachedMouseEvent = false ;
/// <summary>
/// MudColor list of predefined colors. The first five colors will show up as the quick colors on preview dot click.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public IEnumerable < Color > Palette { get ; set ; } = new Color [ ]
{ "#424242" , "#2196f3" , "#00c853" , "#ff9800" , "#f44336" ,
"#f6f9fb" , "#9df1fa" , "#bdffcf" , "#fff0a3" , "#ffd254" ,
"#e6e9eb" , "#27dbf5" , "#7ef7a0" , "#ffe273" , "#ffb31f" ,
"#c9cccf" , "#13b8e8" , "#14dc71" , "#fdd22f" , "#ff9102" ,
"#858791" , "#0989c2" , "#1bbd66" , "#ebb323" , "#fe6800" ,
"#585b62" , "#17698e" , "#17a258" , "#d9980d" , "#dc3f11" ,
"#353940" , "#113b53" , "#127942" , "#bf7d11" , "#aa0000"
} ;
private IEnumerable < Color > _gridList = new Color [ ]
protected override void OnPickerOpened ( )
{
{
"#FFFFFF" , "#ebebeb" , "#d6d6d6" , "#c2c2c2" , "#adadad" , "#999999" , "#858586" , "#707070" , "#5c5c5c" , "#474747" , "#333333" , "#000000" ,
base . OnPickerOpened ( ) ;
"#133648" , "#071d53" , "#0f0638" , "#2a093b" , "#370c1b" , "#541107" , "#532009" , "#53350d" , "#523e0f" , "#65611b" , "#505518" , "#2b3d16" ,
_attachedMouseEvent = true ;
"#1e4c63" , "#0f2e76" , "#180b4e" , "#3f1256" , "#4e1629" , "#781e0e" , "#722f10" , "#734c16" , "#73591a" , "#8c8629" , "#707625" , "#3f5623" ,
StateHasChanged ( ) ;
"#2e6c8c" , "#1841a3" , "#280c72" , "#591e77" , "#6f223d" , "#a62c17" , "#a0451a" , "#a06b23" , "#9f7d28" , "#c3bc3c" , "#9da436" , "#587934" ,
}
"#3c8ab0" , "#2155ce" , "#331c8e" , "#702898" , "#8d2e4f" , "#d03a20" , "#ca5a24" , "#c8862e" , "#c99f35" , "#f3ec4e" , "#c6d047" , "#729b44" ,
"#479fd3" , "#2660f5" , "#4725ab" , "#8c33b5" , "#aa395d" , "#eb512e" , "#ed732e" , "#f3ae3d" , "#f5c944" , "#fefb67" , "#ddeb5c" , "#86b953" ,
"#59c4f7" , "#4e85f6" , "#5733e2" , "#af43eb" , "#d44a7a" , "#ed6c59" , "#ef8c56" , "#f3b757" , "#f6cd5b" , "#fef881" , "#e6ee7a" , "#a3d16e" ,
"#78d3f8" , "#7fa6f8" , "#7e52f5" , "#c45ff6" , "#de789d" , "#f09286" , "#f2a984" , "#f6c983" , "#f9da85" , "#fef9a1" , "#ebf29b" , "#badc94" ,
"#a5e1fa" , "#adc5fa" , "#ab8df7" , "#d696f8" , "#e8a7bf" , "#f4b8b1" , "#f6c7af" , "#f9daae" , "#fae5af" , "#fefbc0" , "#f3f7be" , "#d2e7ba" ,
"#d2effd" , "#d6e1fc" , "#d6c9fa" , "#e9cbfb" , "#f3d4df" , "#f9dcd9" , "#fae3d8" , "#fcecd7" , "#fdf2d8" , "#fefce0" , "#f7fade" , "#e3edd6"
} ;
/// <summary>
protected override void OnPickerClosed ( )
/// When set to true, no mouse move events in the spectrum mode will be captured, so the selector circle won't fellow the mouse.
{
/// Under some conditions like long latency the visual representation might not reflect the user behaviour anymore. So, it can be disabled
base . OnPickerClosed ( ) ;
/// Enabled by default
RemoveMouseOverEvent ( ) . AndForget ( ) ;
/// </summary>
}
[Parameter]
private void HandleColorOverlayClicked ( )
[Category(CategoryTypes.FormComponent.PickerBehavior)]
{
public bool DisableDragEffect { get ; set ; } = false ;
UpdateColorBaseOnSelection ( ) ;
/// <summary>
if ( IsAnyControlVisible ( ) = = false )
/// Custom close icon.
{
/// </summary>
Close ( ) ;
[Parameter]
}
[Category(CategoryTypes.FormComponent.PickerAppearance)]
}
public string CloseIcon { get ; set ; } = Icons . Material . Filled . Close ;
/// <summary>
private void OnSelectorClicked ( MouseEventArgs e )
/// Custom spectrum icon.
{
/// </summary>
SetSelectorBasedOnMouseEvents ( e , false ) ;
[Parameter]
HandleColorOverlayClicked ( ) ;
[Category(CategoryTypes.FormComponent.PickerAppearance)]
}
public string SpectrumIcon { get ; set ; } = Icons . Material . Filled . Tune ;
/// <summary>
private void OnColorOverlayClick ( MouseEventArgs e )
/// Custom grid icon.
{
/// </summary>
SetSelectorBasedOnMouseEvents ( e , true ) ;
[Parameter]
HandleColorOverlayClicked ( ) ;
[Category(CategoryTypes.FormComponent.PickerAppearance)]
}
public string GridIcon { get ; set ; } = Icons . Material . Filled . Apps ;
/// <summary>
private void OnMouseOver ( MouseEventArgs e )
/// Custom palette icon.
{
/// </summary>
if ( e . Buttons = = 1 )
[Parameter]
{
[Category(CategoryTypes.FormComponent.PickerAppearance)]
SetSelectorBasedOnMouseEvents ( e , true ) ;
public string PaletteIcon { get ; set ; } = Icons . Material . Filled . Palette ;
UpdateColorBaseOnSelection ( ) ;
}
}
/// <summary>
private void SetSelectorBasedOnMouseEvents ( MouseEventArgs e , bool offsetIsAbsolute )
/// Custom import/export icont.
{
/// </summary>
_selectorX = ( offsetIsAbsolute = = true ? e . OffsetX : ( e . OffsetX - _selctorSize / 2.0 ) + _selectorX ) . EnsureRange ( _maxX ) ;
[Parameter]
_selectorY = ( offsetIsAbsolute = = true ? e . OffsetY : ( e . OffsetY - _selctorSize / 2.0 ) + _selectorY ) . EnsureRange ( _maxY ) ;
[Category(CategoryTypes.FormComponent.PickerAppearance)]
}
public string ImportExportIcon { get ; set ; } = Icons . Material . Filled . ImportExport ;
# endregion
[Parameter] public EventCallback < Color > ValueChanged { get ; set ; }
private void ToggleCollection ( )
private void ToggleCollection ( )
{
{
@ -391,156 +332,210 @@ public partial class ColorPicker : Picker<Color>, IAsyncDisposable
_selectorX = relation * _maxX ;
_selectorX = relation * _maxX ;
}
}
#region mouse interactions
# endregion
private void HandleColorOverlayClicked ( )
#region Styling
{
UpdateColorBaseOnSelection ( ) ;
if ( IsAnyControlVisible ( ) = = false )
private bool IsAnyControlVisible ( ) = > ! ( DisablePreview & & DisableSliders & & DisableInputs ) ;
{
private ThemeColor GetButtonColor ( ColorPickerView view ) = > _activeColorPickerView = = view ? ThemeColor . Primary : ThemeColor . Inherit ;
Close ( ) ;
private string GetColorDotClass ( Color color ) = > new CssBuilder ( "picker-color-dot" ) . AddClass ( "selected" , color = = Value ) . ToString ( ) ;
}
private string AlphaSliderStyle = > new StyleBuilder ( ) . AddStyle ( $"background-image: linear-gradient(to {(RightToLeft ? " left " : " right ")}, transparent, {_color.ToString(ColorOutputFormats.RGB)})" ) . Build ( ) ;
}
private string GetSelectorLocation ( ) = > $"translate({Math.Round(_selectorX, 2).ToString(CultureInfo.InvariantCulture)}px, {Math.Round(_selectorY, 2).ToString(CultureInfo.InvariantCulture)}px);" ;
private string GetColorTextValue ( ) = > ( DisableAlpha = = true | | _activeColorPickerView is ColorPickerView . Palette or ColorPickerView . GridCompact ) ? _color . ToString ( ColorOutputFormats . Hex ) : _color . ToString ( ColorOutputFormats . HexA ) ;
private int GetHexColorInputMaxLength ( ) = > DisableAlpha ? 7 : 9 ;
private void OnSelectorClicked ( MouseEventArgs e )
/// <summary>
{
/// When set to true, no mouse move events in the spectrum mode will be captured, so the selector circle won't fellow the mouse.
SetSelectorBasedOnMouseEvents ( e , false ) ;
/// Under some conditions like long latency the visual representation might not reflect the user behaviour anymore. So, it can be disabled
HandleColorOverlayClicked ( ) ;
/// Enabled by default
}
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableDragEffect { get ; set ; } = false ;
private void OnColorOverlayClick ( MouseEventArgs e )
/// <summary>
{
/// Custom close icon.
SetSelectorBasedOnMouseEvents ( e , true ) ;
/// </summary>
HandleColorOverlayClicked ( ) ;
[Parameter]
}
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string CloseIcon { get ; set ; } = Icons . Material . Filled . Close ;
private void OnMouseOver ( MouseEventArgs e )
/// <summary>
/// Custom spectrum icon.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string SpectrumIcon { get ; set ; } = Icons . Material . Filled . Tune ;
/// <summary>
/// Custom grid icon.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string GridIcon { get ; set ; } = Icons . Material . Filled . Apps ;
/// <summary>
/// Custom palette icon.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string PaletteIcon { get ; set ; } = Icons . Material . Filled . Palette ;
/// <summary>
/// Custom import/export icont.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string ImportExportIcon { get ; set ; } = Icons . Material . Filled . ImportExport ;
# endregion
#region Behavior
[CascadingParameter(Name = "RightToLeft")] public bool RightToLeft { get ; set ; }
/// <summary>
/// If true, Alpha options will not be displayed and color output will be RGB, HSL or HEX and not RGBA, HSLA or HEXA.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableAlpha
{
{
if ( e . Buttons = = 1 )
get = > _disableAlpha ;
set
{
{
SetSelectorBasedOnMouseEvents ( e , true ) ;
if ( value ! = _disableAlpha )
UpdateColorBaseOnSelection ( ) ;
{
}
_disableAlpha = value ;
}
private void SetSelectorBasedOnMouseEvents ( MouseEventArgs e , bool offsetIsAbsolute )
if ( value = = tru e)
{
{
_selectorX = ( offsetIsAbsolute = = true ? e . OffsetX : ( e . OffsetX - _selctorSize / 2.0 ) + _selectorX ) . EnsureRange ( _maxX ) ;
Value = Value . SetAlpha ( 1.0 ) ;
_selectorY = ( offsetIsAbsolute = = true ? e . OffsetY : ( e . OffsetY - _selctorSize / 2.0 ) + _selectorY ) . EnsureRange ( _maxY ) ;
}
}
# endregion
Text = GetColorTextValue ( ) ;
}
#region updating inputs
}
}
/// <summary>
/// <summary>
/// Set the R (red) component of the color picker
/// If true, the color field will not be displayed.
/// </summary>
/// </summary>
/// <param name="value">A value between 0 (no red) or 255 (max red)</param>
[Parameter]
public void SetR ( int value ) = > Value = Value . SetR ( value ) ;
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableColorField { get ; set ; } = false ;
/// <summary>
/// <summary>
/// Set the G (green) component of the color picker
/// If true, the switch to change color mode will not be displayed.
/// </summary>
/// </summary>
/// <param name="value">A value between 0 (no green) or 255 (max green)</param>
[Parameter]
public void SetG ( int value ) = > Value = Value . SetG ( value ) ;
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableModeSwitch { get ; set ; } = false ;
/// <summary>
/// <summary>
/// Set the B (blue) component of the color picker
/// If true, textfield inputs and color mode switch will not be displayed.
/// </summary>
/// </summary>
/// <param name="value">A value between 0 (no blue) or 255 (max blue)</param>
[Parameter]
public void SetB ( int value ) = > Value = Value . SetB ( value ) ;
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableInputs { get ; set ; } = false ;
/// <summary>
/// <summary>
/// Set the H (hue) component of the color picker
/// If true, hue and alpha sliders will not be displayed.
/// </summary>
/// </summary>
/// <param name="value">A value between 0 and 360 (degrees)</param>
[Parameter]
public void SetH ( double value ) = > Value = Value . SetH ( value ) ;
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableSliders { get ; set ; } = false ;
/// <summary>
/// <summary>
/// Set the S (saturation) component of the color picker
/// If true, the preview color box will not be displayed, note that the preview color functions as a button as well for collection colors.
/// </summary>
/// </summary>
/// <param name="value">A value between 0.0 (no saturation) and 1.0 (max saturation)</param>
[Parameter]
public void SetS ( double value ) = > Value = Value . SetS ( value ) ;
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisablePreview { get ; set ; } = false ;
/// <summary>
/// <summary>
/// Set the L (Lightness) component of the color picker
/// The initial mode (RGB, HSL or HEX) the picker should open. Defaults to RGB
/// </summary>
/// </summary>
/// <param name="value">A value between 0.0 (no light, black) and 1.0 (max light, white)</param>
[Parameter]
public void SetL ( double value ) = > Value = Value . SetL ( value ) ;
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public ColorPickerMode ColorPickerMode { get ; set ; } = ColorPickerMode . RGB ;
/// <summary>
/// <summary>
/// Set the Alpha (transparency) component of the color picker
/// The initial view of the picker. Views can be changed if toolbar is enabled.
/// </summary>
/// </summary>
/// <param name="value">A value between 0.0 (full transparent) and 1.0 (solid) </param>
[Parameter]
public void SetAlpha ( double value ) = > Value = Value . SetAlpha ( value ) ;
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public ColorPickerView ColorPickerView
{
get = > _colorPickerView ;
set
{
if ( value ! = _colorPickerView )
{
_colorPickerView = value ;
ChangeView ( value ) . AndForget ( ) ;
}
}
}
/// <summary>
/// <summary>
/// Set the Alpha (transparency) component of the color picker
/// If true, binding changes occurred also when HSL values changed without a corresponding RGB change
/// </summary>
/// </summary>
/// <param name="value">A value between 0 (full transparent) and 1 (solid) </param>
[Parameter]
public void SetAlpha ( int value ) = > Value = Value . SetAlpha ( value ) ;
[Category(CategoryTypes.FormComponent.Behavior)]
public bool UpdateBindingIfOnlyHSLChanged { get ; set ; } = false ;
/// <summary>
/// <summary>
/// Set the color of the picker based on the string input
/// A two-way bindable property representing the selected value. MudColor is a utility class that can be used to get the value as RGB, HSL, hex or other value
/// </summary>
/// </summary>
/// <param name="input">Accepting different formats for a color representation such as rbg, rgba, #</param>
[Parameter]
public void SetInputString ( string input )
[Category(CategoryTypes.FormComponent.Data)]
{
public Color Value
Color color ;
try
{
{
color = new Color ( input ) ;
get = > _color ;
}
set
catch ( Exception )
{
{
return ;
if ( value = = null ) { return ; }
}
Value = color ;
var rgbChanged = value ! = _color ;
}
var hslChanged = _color = = null ? false : value . HslChanged ( _color ) ;
_color = value ;
protected override Task StringValueChanged ( string value )
if ( rgbChanged )
{
{
SetInputString ( value ) ;
if ( _skipFeedback = = false )
return Task . CompletedTask ;
{
UpdateBaseColor ( ) ;
UpdateColorSelectorBasedOnRgb ( ) ;
}
}
private bool _attachedMouseEvent = false ;
SetTextAsync ( GetColorTextValue ( ) , false ) . AndForget ( ) ;
ValueChanged . InvokeAsync ( value ) . AndForget ( ) ;
protected override void OnPickerOpened ( )
FieldChanged ( value ) ;
{
base . OnPickerOpened ( ) ;
_attachedMouseEvent = true ;
StateHasChanged ( ) ;
}
}
protected override void OnPickerClosed ( )
if ( rgbChanged = = false & & UpdateBindingIfOnlyHSLChanged & & hslChanged = = true )
{
{
base . OnPickerClosed ( ) ;
SetTextAsync ( GetColorTextValue ( ) , false ) . AndForget ( ) ;
RemoveMouseOverEvent ( ) . AndForget ( ) ;
ValueChanged . InvokeAsync ( value ) . AndForget ( ) ;
FieldChanged ( value ) ;
}
}
}
}
# endregion
#region helper
private string GetSelectorLocation ( ) = > $"translate({Math.Round(_selectorX, 2).ToString(CultureInfo.InvariantCulture)}px, {Math.Round(_selectorY, 2).ToString(CultureInfo.InvariantCulture)}px);" ;
private string GetColorTextValue ( ) = > ( DisableAlpha = = true | | _activeColorPickerView is ColorPickerView . Palette or ColorPickerView . GridCompact ) ? _color . ToString ( ColorOutputFormats . Hex ) : _color . ToString ( ColorOutputFormats . HexA ) ;
private int GetHexColorInputMaxLength ( ) = > DisableAlpha ? 7 : 9 ;
private EventCallback < MouseEventArgs > GetEventCallback ( ) = > EventCallback . Factory . Create < MouseEventArgs > ( this , ( ) = > Close ( ) ) ;
private bool IsAnyControlVisible ( ) = > ! ( DisablePreview & & DisableSliders & & DisableInputs ) ;
private EventCallback < MouseEventArgs > GetSelectPaletteColorCallback ( Color color ) = > new EventCallbackFactory ( ) . Create ( this , ( MouseEventArgs e ) = > SelectPaletteColor ( color ) ) ;
private ThemeColor GetButtonColor ( ColorPickerView view ) = > _activeColorPickerView = = view ? ThemeColor . Primary : ThemeColor . Inherit ;
private string GetColorDotClass ( Color color ) = > new CssBuilder ( "picker-color-dot" ) . AddClass ( "selected" , color = = Value ) . ToString ( ) ;
private string AlphaSliderStyle = > new StyleBuilder ( ) . AddStyle ( $"background-image: linear-gradient(to {(RightToLeft ? " left " : " right ")}, transparent, {_color.ToString(ColorOutputFormats.RGB)})" ) . Build ( ) ;
# endregion
# endregion
#region life cycle hooks
#region Lifecycle
public ColorPicker ( ) : base ( new DefaultConverter < Color > ( ) )
{
AdornmentIcon = Icons . Material . Outlined . Palette ;
DisableToolbar = true ;
Value = "#594ae2" ; //MudBlazor Blue
Text = GetColorTextValue ( ) ;
AdornmentAriaLabel = "Open Color Picker" ;
}
protected override async Task OnAfterRenderAsync ( bool firstRender )
protected override async Task OnAfterRenderAsync ( bool firstRender )
{
{
@ -594,4 +589,5 @@ public partial class ColorPicker : Picker<Color>, IAsyncDisposable
}
}
# endregion
# endregion
}
}