import * as CodeCityVis from "codecity-visualizer" ; import BuildingColorTheme from "../../classes/BuildingColorTheme" ; import { MetricScale } from "./LayoutProcessor" ; const attributeHelper = CodeCityVis . helper . attributes ; class LayoutBuildingColorRules { private readonly _colorMode : BuildingColorTheme ; private readonly _metricScale : MetricScale ; public constructor ( metricScale , colorMode ) { this . _metricScale = metricScale ; this . _colorMode = colorMode ; } returns { BaseRule } public ruleBuildingColorInitial ( ) { return new CodeCityVis . rules . universal ( { condition : ( model , node ) => ! ! ( model && node . children . length === 0 && node . parent ) , metric : ( ) => { return this . _colorMode . defaultColor ; } , attributes : "color" , } ) ; } private returns { BaseRule } public ruleBuildingColorByPackageName ( ) { return new CodeCityVis . rules . color . assigned ( { condition : ( model , node ) => ! ! ( model && node . children . length === 0 && node . parent ) , metric : ( model , node ) : string => { return model && String ( node . parent ) ; } , attributes : "color" , } ) ; } private returns { BaseRule } public ruleBuildingColorByLinesOfCode ( ) { const minVal = 25 ; let maxVal = this . _metricScale . metricColor . max ; maxVal = Math . max ( 350 , maxVal ) ; maxVal = Math . min ( 900 , maxVal ) ; return this . createMinMaxRule ( minVal , maxVal , this . _colorMode , true ) ; } public ruleBuildingColorByNumberOfAuthors ( ) { const minVal = 1 ; const maxVal = 3 ; return this . createMinMaxRule ( minVal , maxVal , this . _colorMode , false ) ; } public ruleBuildingColorByNumberOfCommits ( ) { const minVal = 1 ; const maxVal = 5 ; return this . createMinMaxRule ( minVal , maxVal , this . _colorMode , true ) ; } private returns { BaseRule } public ruleBuildingColorByComplexity ( ) { const minVal = 1 ; const maxVal = this . _metricScale . metricColor . max ; return this . createMinMaxRule ( minVal , maxVal , this . _colorMode , true ) ; } private returns { BaseRule } public ruleBuildingColorByCoverage ( ) { const minVal = 0 ; const maxVal = 95 ; return this . createMinMaxRule ( minVal , maxVal , this . _colorMode , false ) ; } private returns { BaseRule } public ruleBuildingColorByIssues ( ) { const minVal = 0 ; let maxVal = this . _metricScale . metricColor . max ; maxVal = Math . max ( 20 , maxVal ) ; maxVal = Math . min ( 180 , maxVal ) ; return this . createMinMaxRule ( minVal , maxVal , this . _colorMode , true ) ; } private returns { BaseRule } public ruleBuildingColorByOpenIssues ( ) { const minVal = 0 ; let maxVal = this . _metricScale . metricColor . max ; maxVal = Math . max ( 20 , maxVal ) ; maxVal = Math . min ( 180 , maxVal ) ; return this . createMinMaxRule ( minVal , maxVal , this . _colorMode , true ) ; } private createMinMaxRule ( minVal , maxVal , colorMode , isMinGood ) { return new CodeCityVis . rules . color . gradient ( { condition : ( model , node ) => model && node . children . length === 0 , metric : ( model , node , version ) => { const attr = attributeHelper . attrFallbackSweep ( model , node , version ) ; return "metricColor" in attr ? attr [ "metricColor" ] : 0 ; } , attributes : "color" , min : minVal , max : maxVal , minColor : isMinGood ? colorMode . goodColor : colorMode . badColor , maxColor : isMinGood ? colorMode . badColor : colorMode . goodColor , } ) ; } } export default LayoutBuildingColorRules ;