@@ -10,11 +10,11 @@ import { Guid } from '@ts/core/m_guid';
1010import { extend } from '@ts/core/utils/m_extend' ;
1111import { getHeight , getOuterHeight , getWidth } from '@ts/core/utils/m_size' ;
1212import type { OptionChanged } from '@ts/core/widget/types' ;
13- import type { SupportedKeys } from '@ts/core/widget/widget' ;
13+ import type { SupportedKeyHandler , SupportedKeys } from '@ts/core/widget/widget' ;
1414import eventsEngine from '@ts/events/core/m_events_engine' ;
1515import { name as clickEventName } from '@ts/events/m_click' ;
1616import { isCommandKeyPressed } from '@ts/events/utils/index' ;
17- import Color from '@ts/m_color' ;
17+ import Color , { type ColorInstance } from '@ts/m_color' ;
1818import Draggable from '@ts/m_draggable' ;
1919import type { EditorProperties , ValueChangedEvent } from '@ts/ui/editor/editor' ;
2020import Editor from '@ts/ui/editor/editor' ;
@@ -85,6 +85,23 @@ export interface ColorViewProperties extends EditorProperties {
8585 target ?: string | Element | dxElementWrapper | null ;
8686}
8787
88+ type EditorWithLabelType = new (
89+ element : dxElementWrapper ,
90+ options ?: object ,
91+ ) => { registerKeyHandler : ( key : string , handler : SupportedKeyHandler ) => void } ;
92+
93+ interface EditorWithLabelOptions {
94+ editorType : EditorWithLabelType ;
95+ value : number | string ;
96+ onValueChanged : ( args : ValueChangedEvent ) => void ;
97+ labelText : string ;
98+ labelAriaText : string ;
99+ labelClass : string ;
100+ min ?: number ;
101+ max ?: number ;
102+ step ?: number ;
103+ }
104+
88105class ColorView extends Editor < ColorViewProperties > {
89106 _$palette ! : dxElementWrapper ;
90107
@@ -100,8 +117,7 @@ class ColorView extends Editor<ColorViewProperties> {
100117
101118 _updateByDrag ?: boolean ;
102119
103- // need typings and correct class in m_color.ts
104- _currentColor ! : any ;
120+ _currentColor ! : ColorInstance ;
105121
106122 _isTopColorHue ?: boolean ;
107123
@@ -401,19 +417,11 @@ class ColorView extends Editor<ColorViewProperties> {
401417 this . _renderAlphaChannelElements ( ) ;
402418 }
403419
404- _makeTransparentBackground ( $el : dxElementWrapper , color : any ) : void {
405- if ( ! ( color instanceof Color ) ) {
406- color = new Color ( color ) ;
407- }
408-
420+ _makeTransparentBackground ( $el : dxElementWrapper , color : ColorInstance ) : void {
409421 $el . css ( 'backgroundColor' , this . _makeRgba ( color ) ) ;
410422 }
411423
412- _makeRgba ( color : any ) : string {
413- if ( ! ( color instanceof Color ) ) {
414- color = new Color ( color ) ;
415- }
416-
424+ _makeRgba ( color : ColorInstance ) : string {
417425 return `rgba(${ [ color . r , color . g , color . b , color . a ] . join ( ', ' ) } )` ;
418426 }
419427
@@ -438,15 +446,12 @@ class ColorView extends Editor<ColorViewProperties> {
438446 if ( delta < 0 ) {
439447 delta = Math . abs ( delta ) ;
440448 const rows : dxElementWrapper [ ] = [ ] ;
441- let i ;
442- for ( i = 0 ; i < delta ; i ++ ) {
449+ for ( let i = 0 ; i < delta ; i += 1 ) {
443450 rows . push ( $ ( '<div>' ) . addClass ( COLOR_VIEW_ROW_CLASS ) ) ;
444451 }
445452
446453 if ( renderedRowsCount ) {
447- for ( i = 0 ; i < rows . length ; i ++ ) {
448- $renderedRows . eq ( 0 ) . after ( rows [ i ] ) ;
449- }
454+ rows . forEach ( ( row ) => { $renderedRows . eq ( 0 ) . after ( row ) ; } ) ;
450455 } else {
451456 this . _$colorPickerContainer . append ( rows ) ;
452457 }
@@ -539,12 +544,12 @@ class ColorView extends Editor<ColorViewProperties> {
539544
540545 _calculateColorValue ( paletteHandlePosition : PaletteHandlePosition ) : number {
541546 const value = Math . floor ( paletteHandlePosition . top + this . _paletteHandleHeight / 2 ) ;
542- return 100 - Math . round ( value * 100 / this . _paletteHeight ) ;
547+ return 100 - Math . round ( ( value * 100 ) / this . _paletteHeight ) ;
543548 }
544549
545550 _calculateColorSaturation ( paletteHandlePosition : PaletteHandlePosition ) : number {
546551 const saturation = Math . floor ( paletteHandlePosition . left + this . _paletteHandleWidth / 2 ) ;
547- return Math . round ( saturation * 100 / this . _paletteWidth ) ;
552+ return Math . round ( ( saturation * 100 ) / this . _paletteWidth ) ;
548553 }
549554
550555 _updateColorFromHsv ( hue : number , saturation : number , value : number ) : void {
@@ -603,7 +608,7 @@ class ColorView extends Editor<ColorViewProperties> {
603608 _placeHueScaleHandle ( ) : void {
604609 const hueScaleHeight = this . _hueScaleWrapperHeight ;
605610 const handleHeight = this . _hueScaleHandleHeight ;
606- let top = ( hueScaleHeight - handleHeight ) * ( 360 - this . _currentColor . hsv . h ) / 360 ;
611+ let top = ( ( hueScaleHeight - handleHeight ) * ( 360 - this . _currentColor . hsv . h ) ) / 360 ;
607612
608613 if ( hueScaleHeight < top + handleHeight ) {
609614 top = hueScaleHeight - handleHeight ;
@@ -662,7 +667,7 @@ class ColorView extends Editor<ColorViewProperties> {
662667 this . _$currentColor = $ ( '<div>' ) . addClass ( [ COLOR_VIEW_COLOR_PREVIEW , COLOR_VIEW_COLOR_PREVIEW_COLOR_NEW ] . join ( ' ' ) ) ;
663668 this . _$baseColor = $ ( '<div>' ) . addClass ( [ COLOR_VIEW_COLOR_PREVIEW , COLOR_VIEW_COLOR_PREVIEW_COLOR_CURRENT ] . join ( ' ' ) ) ;
664669
665- this . _makeTransparentBackground ( this . _$baseColor , matchValue ) ;
670+ this . _makeTransparentBackground ( this . _$baseColor , new Color ( matchValue ?? BLACK_COLOR ) ) ;
666671 this . _makeTransparentBackground ( this . _$currentColor , this . _currentColor ) ;
667672
668673 $colorsPreviewContainerInner . append ( [ this . _$baseColor , this . _$currentColor ] ) ;
@@ -718,7 +723,7 @@ class ColorView extends Editor<ColorViewProperties> {
718723 ] ;
719724 }
720725
721- _renderEditorWithLabel ( options ) : dxElementWrapper {
726+ _renderEditorWithLabel ( options : EditorWithLabelOptions ) : dxElementWrapper {
722727 const $editor = $ ( '<div>' ) ;
723728 const $label = $ ( '<label>' )
724729 . addClass ( options . labelClass )
@@ -747,7 +752,7 @@ class ColorView extends Editor<ColorViewProperties> {
747752 editorOptions . step = options . step || 1 ;
748753 }
749754
750- const editor = new EditorConstructor ( $editor , editorOptions ) ;
755+ const editor = new ( EditorConstructor ) ( $editor , editorOptions ) ;
751756
752757 editor . registerKeyHandler ( 'enter' , ( e ) => {
753758 this . _fireEnterKeyPressed ( e ) ;
@@ -758,7 +763,7 @@ class ColorView extends Editor<ColorViewProperties> {
758763 return $label ;
759764 }
760765
761- hexInputOptions ( ) {
766+ hexInputOptions ( ) : EditorWithLabelOptions {
762767 return {
763768 editorType : TextBox ,
764769 value : this . _currentColor . toHex ( ) . replace ( '#' , '' ) ,
@@ -829,7 +834,9 @@ class ColorView extends Editor<ColorViewProperties> {
829834 onValueChanged : ( args ) => {
830835 let { value } = args ;
831836 value = this . _currentColor . isValidAlpha ( value ) ? value : this . _currentColor . a ;
832- args . event && this . _saveValueChangeEvent ( args . event ) ;
837+ if ( args . event ) {
838+ this . _saveValueChangeEvent ( args . event ) ;
839+ }
833840 this . _updateColorTransparency ( value ) ;
834841 this . _placeAlphaChannelHandle ( ) ;
835842 } ,
@@ -860,7 +867,8 @@ class ColorView extends Editor<ColorViewProperties> {
860867 onDragMove : ( { event } ) => {
861868 this . _updateByDrag = true ;
862869 const $alphaChannelHandle = this . _$alphaChannelHandle ;
863- const alphaChannelHandlePosition = locate ( $alphaChannelHandle ) . left + this . _alphaChannelHandleWidth / 2 ;
870+ const alphaChannelHandlePosition = locate ( $alphaChannelHandle ) . left
871+ + this . _alphaChannelHandleWidth / 2 ;
864872 this . _saveValueChangeEvent ( event ) ;
865873 this . _calculateColorTransparencyByScaleWidth ( alphaChannelHandlePosition ) ;
866874 } ,
@@ -934,15 +942,21 @@ class ColorView extends Editor<ColorViewProperties> {
934942 }
935943
936944 _updateColor ( isHex : boolean , args : ValueChangedEvent ) : void {
937- let rgba ;
945+ let rgba : number [ ] = [ ] ;
938946 let newColor = '' ;
939947
940948 if ( isHex ) {
941949 newColor = this . _validateHex ( `#${ this . _hexInput . option ( 'value' ) as string } ` ) ;
942950 } else {
943951 rgba = this . _validateRgb ( ) ;
944952 if ( this . _alphaChannelInput ) {
945- rgba . push ( this . _alphaChannelInput . option ( 'value' ) ) ;
953+ const { value : alphaValue } = this . _alphaChannelInput . option ( ) ;
954+ const isValidAlpha = alphaValue !== undefined
955+ && this . _currentColor . isValidAlpha ( alphaValue ) ;
956+
957+ const valueToAdd = isValidAlpha ? alphaValue : this . _currentColor . a ;
958+
959+ rgba . push ( valueToAdd ) ;
946960 newColor = `rgba(${ rgba . join ( ', ' ) } )` ;
947961 } else {
948962 newColor = `rgb(${ rgba . join ( ', ' ) } )` ;
@@ -958,15 +972,16 @@ class ColorView extends Editor<ColorViewProperties> {
958972 }
959973
960974 _validateHex ( hex : string ) : string {
961- return this . _currentColor . isValidHex ( hex ) ? hex : this . _currentColor . toHex ( ) as string ;
975+ return this . _currentColor . isValidHex ( hex ) ? hex : this . _currentColor . toHex ( ) ;
962976 }
963977
964978 _validateRgb ( ) : number [ ] {
965979 let { value : r } = this . _rgbInputs [ 0 ] . option ( ) ;
966980 let { value : g } = this . _rgbInputs [ 1 ] . option ( ) ;
967981 let { value : b } = this . _rgbInputs [ 2 ] . option ( ) ;
968982
969- if ( ! this . _currentColor . isValidRGB ( r , g , b ) ) {
983+ const isInvalidRgb = ! this . _currentColor . isValidRGB ( r , g , b ) ;
984+ if ( isInvalidRgb ) {
970985 r = this . _currentColor . r ;
971986 g = this . _currentColor . g ;
972987 b = this . _currentColor . b ;
0 commit comments