@ -13,16 +13,7 @@ namespace Connected.Components;
public partial class ColorPicker : Picker < Color > , IAsyncDisposable
{
public ColorPicker ( ) : base ( new DefaultConverter < Color > ( ) )
{
AdornmentIcon = Icons . Material . Outlined . Palette ;
DisableToolbar = true ;
Value = "#594ae2" ; //MudBlazor Blue
Text = GetColorTextValue ( ) ;
AdornmentAriaLabel = "Open Color Picker" ;
}
#region Fields
#region Variables
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 Guid _throttledMouseOverEventId ;
private IEventListener _throttledEventManager ;
[Inject] IEventListenerFactory ThrottledEventManagerFactory { get ; set ; }
# endregion
#region Parameters
[CascadingParameter(Name = "RightToLeft")] public bool RightToLeft { get ; set ; }
private ColorPickerView _colorPickerView = ColorPickerView . Spectrum ;
private ColorPickerView _activeColorPickerView = ColorPickerView . Spectrum ;
private bool _disableAlpha = false ;
private IEventListener _throttledEventManager ;
/// <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>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableAlpha
{
get = > _disableAlpha ;
set
{
if ( value ! = _disableAlpha )
{
_disableAlpha = value ;
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"
} ;
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>
/// If true, the color field will not be displayed.
/// Set the R (red) component of the color picker
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableColorField { get ; set ; } = false ;
/// <param name="value">A value between 0 (no red) or 255 (max red)</param>
public void SetR ( int value ) = > Value = Value . SetR ( value ) ;
/// <summary>
/// If true, the switch to change color mode will not be displayed.
/// Set the G (green) component of the color picker
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableModeSwitch { get ; set ; } = false ;
/// <param name="value">A value between 0 (no green) or 255 (max green)</param>
public void SetG ( int value ) = > Value = Value . SetG ( value ) ;
/// <summary>
/// If true, textfield inputs and color mode switch will not be displayed.
/// Set the B (blue) component of the color picker
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableInputs { get ; set ; } = false ;
/// <param name="value">A value between 0 (no blue) or 255 (max blue)</param>
public void SetB ( int value ) = > Value = Value . SetB ( value ) ;
/// <summary>
/// If true, hue and alpha sliders will not be displayed.
/// Set the H (hue) component of the color picker
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableSliders { get ; set ; } = false ;
/// <param name="value">A value between 0 and 360 (degrees)</param>
public void SetH ( double value ) = > Value = Value . SetH ( value ) ;
/// <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>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisablePreview { get ; set ; } = false ;
/// <param name="value">A value between 0.0 (no saturation) and 1.0 (max saturation)</param>
public void SetS ( double value ) = > Value = Value . SetS ( value ) ;
/// <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>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public ColorPickerMode ColorPickerMode { get ; set ; } = ColorPickerMode . RGB ;
private ColorPickerView _colorPickerView = ColorPickerView . Spectrum ;
private ColorPickerView _activeColorPickerView = ColorPickerView . Spectrum ;
/// <param name="value">A value between 0.0 (no light, black) and 1.0 (max light, white)</param>
public void SetL ( double value ) = > Value = Value . SetL ( value ) ;
/// <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>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public ColorPickerView ColorPickerView
{
get = > _colorPickerView ;
set
{
if ( value ! = _colorPickerView )
{
_colorPickerView = value ;
ChangeView ( value ) . AndForget ( ) ;
}
}
}
/// <param name="value">A value between 0.0 (full transparent) and 1.0 (solid) </param>
public void SetAlpha ( double value ) = > Value = Value . SetAlpha ( value ) ;
/// <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>
[Parameter]
[Category(CategoryTypes.FormComponent.Behavior)]
public bool UpdateBindingIfOnlyHSLChanged { get ; set ; } = false ;
/// <param name="value">A value between 0 (full transparent) and 1 (solid) </param>
public void SetAlpha ( int value ) = > Value = Value . SetAlpha ( value ) ;
/// <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>
[Parameter]
[Category(CategoryTypes.FormComponent.Data)]
public Color Value
{
get = > _color ;
set
/// <param name="input">Accepting different formats for a color representation such as rbg, rgba, #</param>
public void SetInputString ( string input )
{
if ( value = = null ) { return ; }
var rgbChanged = value ! = _color ;
var hslChanged = _color = = null ? false : value . HslChanged ( _color ) ;
_color = value ;
if ( rgbChanged )
Color color ;
try
{
if ( _skipFeedback = = false )
color = new Color ( input ) ;
}
catch ( Exception )
{
UpdateBaseColor ( ) ;
UpdateColorSelectorBasedOnRgb ( ) ;
return ;
}
SetTextAsync ( GetColorTextValue ( ) , false ) . AndForget ( ) ;
ValueChanged . InvokeAsync ( value ) . AndForget ( ) ;
FieldChanged ( value ) ;
Value = color ;
}
if ( rgbChanged = = false & & UpdateBindingIfOnlyHSLChanged & & hslChanged = = tr ue)
protected override Task StringValueChanged ( string val ue)
{
SetTextAsync ( GetColorTextValue ( ) , false ) . AndForget ( ) ;
ValueChanged . InvokeAsync ( value ) . AndForget ( ) ;
FieldChanged ( value ) ;
}
}
SetInputString ( value ) ;
return Task . CompletedTask ;
}
[Parameter] public EventCallback < Color > ValueChanged { get ; set ; }
/// <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 bool _attachedMouseEvent = false ;
private IEnumerable < Color > _gridList = new Color [ ]
protected override void OnPickerOpened ( )
{
"#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"
} ;
base . OnPickerOpened ( ) ;
_attachedMouseEvent = true ;
StateHasChanged ( ) ;
}
/// <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.
/// Under some conditions like long latency the visual representation might not reflect the user behaviour anymore. So, it can be disabled
/// Enabled by default
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableDragEffect { get ; set ; } = false ;
protected override void OnPickerClosed ( )
{
base . OnPickerClosed ( ) ;
RemoveMouseOverEvent ( ) . AndForget ( ) ;
}
private void HandleColorOverlayClicked ( )
{
UpdateColorBaseOnSelection ( ) ;
/// <summary>
/// Custom close icon.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string CloseIcon { get ; set ; } = Icons . Material . Filled . Close ;
if ( IsAnyControlVisible ( ) = = false )
{
Close ( ) ;
}
}
/// <summary>
/// Custom spectrum icon.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string SpectrumIcon { get ; set ; } = Icons . Material . Filled . Tune ;
private void OnSelectorClicked ( MouseEventArgs e )
{
SetSelectorBasedOnMouseEvents ( e , false ) ;
HandleColorOverlayClicked ( ) ;
}
/// <summary>
/// Custom grid icon.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string GridIcon { get ; set ; } = Icons . Material . Filled . Apps ;
private void OnColorOverlayClick ( MouseEventArgs e )
{
SetSelectorBasedOnMouseEvents ( e , true ) ;
HandleColorOverlayClicked ( ) ;
}
/// <summary>
/// Custom palette icon.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string PaletteIcon { get ; set ; } = Icons . Material . Filled . Palette ;
private void OnMouseOver ( MouseEventArgs e )
{
if ( e . Buttons = = 1 )
{
SetSelectorBasedOnMouseEvents ( e , true ) ;
UpdateColorBaseOnSelection ( ) ;
}
}
/// <summary>
/// Custom import/export icont.
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerAppearance)]
public string ImportExportIcon { get ; set ; } = Icons . Material . Filled . ImportExport ;
private void SetSelectorBasedOnMouseEvents ( MouseEventArgs e , bool offsetIsAbsolute )
{
_selectorX = ( offsetIsAbsolute = = true ? e . OffsetX : ( e . OffsetX - _selctorSize / 2.0 ) + _selectorX ) . EnsureRange ( _maxX ) ;
_selectorY = ( offsetIsAbsolute = = true ? e . OffsetY : ( e . OffsetY - _selctorSize / 2.0 ) + _selectorY ) . EnsureRange ( _maxY ) ;
}
# endregion
[Parameter] public EventCallback < Color > ValueChanged { get ; set ; }
private void ToggleCollection ( )
{
@ -391,156 +332,210 @@ public partial class ColorPicker : Picker<Color>, IAsyncDisposable
_selectorX = relation * _maxX ;
}
#region mouse interactions
# endregion
private void HandleColorOverlayClicked ( )
{
UpdateColorBaseOnSelection ( ) ;
#region Styling
if ( IsAnyControlVisible ( ) = = false )
{
Close ( ) ;
}
}
private bool IsAnyControlVisible ( ) = > ! ( DisablePreview & & DisableSliders & & DisableInputs ) ;
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 ( ) ;
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 )
{
SetSelectorBasedOnMouseEvents ( e , false ) ;
HandleColorOverlayClicked ( ) ;
}
/// <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.
/// Under some conditions like long latency the visual representation might not reflect the user behaviour anymore. So, it can be disabled
/// Enabled by default
/// </summary>
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableDragEffect { get ; set ; } = false ;
private void OnColorOverlayClick ( MouseEventArgs e )
{
SetSelectorBasedOnMouseEvents ( e , true ) ;
HandleColorOverlayClicked ( ) ;
}
/// <summary>
/// Custom close icon.
/// </summary>
[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 ) ;
UpdateColorBaseOnSelection ( ) ;
}
}
if ( value ! = _disableAlpha )
{
_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 ) ;
_selectorY = ( offsetIsAbsolute = = true ? e . OffsetY : ( e . OffsetY - _selctorSize / 2.0 ) + _selectorY ) . EnsureRange ( _maxY ) ;
Value = Value . SetAlpha ( 1.0 ) ;
}
# endregion
#region updating inputs
Text = GetColorTextValue ( ) ;
}
}
}
/// <summary>
/// Set the R (red) component of the color picker
/// If true, the color field will not be displayed.
/// </summary>
/// <param name="value">A value between 0 (no red) or 255 (max red)</param>
public void SetR ( int value ) = > Value = Value . SetR ( value ) ;
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableColorField { get ; set ; } = false ;
/// <summary>
/// Set the G (green) component of the color picker
/// If true, the switch to change color mode will not be displayed.
/// </summary>
/// <param name="value">A value between 0 (no green) or 255 (max green)</param>
public void SetG ( int value ) = > Value = Value . SetG ( value ) ;
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableModeSwitch { get ; set ; } = false ;
/// <summary>
/// Set the B (blue) component of the color picker
/// If true, textfield inputs and color mode switch will not be displayed.
/// </summary>
/// <param name="value">A value between 0 (no blue) or 255 (max blue)</param>
public void SetB ( int value ) = > Value = Value . SetB ( value ) ;
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableInputs { get ; set ; } = false ;
/// <summary>
/// Set the H (hue) component of the color picker
/// If true, hue and alpha sliders will not be displayed.
/// </summary>
/// <param name="value">A value between 0 and 360 (degrees)</param>
public void SetH ( double value ) = > Value = Value . SetH ( value ) ;
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisableSliders { get ; set ; } = false ;
/// <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>
/// <param name="value">A value between 0.0 (no saturation) and 1.0 (max saturation)</param>
public void SetS ( double value ) = > Value = Value . SetS ( value ) ;
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public bool DisablePreview { get ; set ; } = false ;
/// <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>
/// <param name="value">A value between 0.0 (no light, black) and 1.0 (max light, white)</param>
public void SetL ( double value ) = > Value = Value . SetL ( value ) ;
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public ColorPickerMode ColorPickerMode { get ; set ; } = ColorPickerMode . RGB ;
/// <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>
/// <param name="value">A value between 0.0 (full transparent) and 1.0 (solid) </param>
public void SetAlpha ( double value ) = > Value = Value . SetAlpha ( value ) ;
[Parameter]
[Category(CategoryTypes.FormComponent.PickerBehavior)]
public ColorPickerView ColorPickerView
{
get = > _colorPickerView ;
set
{
if ( value ! = _colorPickerView )
{
_colorPickerView = value ;
ChangeView ( value ) . AndForget ( ) ;
}
}
}
/// <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>
/// <param name="value">A value between 0 (full transparent) and 1 (solid) </param>
public void SetAlpha ( int value ) = > Value = Value . SetAlpha ( value ) ;
[Parameter]
[Category(CategoryTypes.FormComponent.Behavior)]
public bool UpdateBindingIfOnlyHSLChanged { get ; set ; } = false ;
/// <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>
/// <param name="input">Accepting different formats for a color representation such as rbg, rgba, #</param>
public void SetInputString ( string input )
{
Color color ;
try
[Parameter]
[Category(CategoryTypes.FormComponent.Data)]
public Color Value
{
color = new Color ( input ) ;
}
catch ( Exception )
get = > _color ;
set
{
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 ) ;
return Task . CompletedTask ;
if ( _skipFeedback = = false )
{
UpdateBaseColor ( ) ;
UpdateColorSelectorBasedOnRgb ( ) ;
}
private bool _attachedMouseEvent = false ;
protected override void OnPickerOpened ( )
{
base . OnPickerOpened ( ) ;
_attachedMouseEvent = true ;
StateHasChanged ( ) ;
SetTextAsync ( GetColorTextValue ( ) , false ) . AndForget ( ) ;
ValueChanged . InvokeAsync ( value ) . AndForget ( ) ;
FieldChanged ( value ) ;
}
protected override void OnPickerClosed ( )
if ( rgbChanged = = false & & UpdateBindingIfOnlyHSLChanged & & hslChanged = = true )
{
base . OnPickerClosed ( ) ;
RemoveMouseOverEvent ( ) . AndForget ( ) ;
SetTextAsync ( GetColorTextValue ( ) , false ) . 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
#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 )
{
@ -594,4 +589,5 @@ public partial class ColorPicker : Picker<Color>, IAsyncDisposable
}
# endregion
}