001package org.nasdanika.html.bootstrap; 002 003import java.util.ArrayList; 004import java.util.Arrays; 005import java.util.List; 006import java.util.stream.Collectors; 007 008/** 009 * Bootstrap colors. Not all colors are applicable in all situations. E.g. TRANSPARENT is applicable for backgrounds. 010 * @author Pavel Vlasov 011 * 012 */ 013public enum Color { 014 015 /** 016 * No color - for borders means that there should be no border. 017 * For other uses is the same as DEFAULT 018 */ 019 NONE(null, ""), 020 /** 021 * Default color - no specific. 022 */ 023 DEFAULT(null, ""), 024 PRIMARY("primary", "Primary"), 025 SECONDARY("secondary", "Secondary"), 026 SUCCESS("success", "Success"), 027 DANGER("danger", "Danger"), 028 WARNING("warning", "Warning"), 029 INFO("info", "Info"), 030 LIGHT("light", "Light"), 031 DARK("dark", "Dark"), 032 BODY("body", "Body", Applicability.TEXT), 033 MUTED("muted", "Muted", Applicability.TEXT), 034 WHITE("white", "White"), 035 BLACK50("black-50", "Black 50%", Applicability.TEXT), 036 WHITE50("white-50", "White 50%", Applicability.TEXT), 037 TRANSPARENT("transparent", "Transparent", Applicability.BACKGROUND); 038 039 /** 040 * Defines where the color can be applied. 041 * @author Pavel 042 * 043 */ 044 public static enum Applicability { 045 BACKGROUND, 046 BORDER, 047 TEXT; 048 049 /** 050 * @return Applicable colors; 051 */ 052 public List<Color> colors() { 053 return Arrays.stream(Color.values()).filter(c -> c.isApplicable(this)).collect(Collectors.toList()); 054 } 055 056 /** 057 * @return Labels of applicable colors with suppressed duplicates - only one empty string for both NONE and DEFAULT. 058 */ 059 public List<Object> colorLabels() { 060 List<Object> ret = new ArrayList<>(); 061 for (Color c: colors()) { 062 if (!ret.contains(c.label)) { 063 ret.add(c.label); 064 } 065 } 066 return ret; 067 } 068 069 } 070 071 Color(String code, String label, Applicability... applicability) { 072 this.code = code; 073 this.label = label; 074 this.applicabilities = applicability; 075 } 076 077 public final String code; 078 public final String label; 079 private final Applicability[] applicabilities; 080 081 public static Color fromLabel(String label) { 082 for (Color color: values()) { 083 if (color.label.equals(label)) { 084 return color; 085 } 086 } 087 throw new IllegalArgumentException("No color value for label "+label); 088 } 089 090 public boolean isApplicable(Applicability applicability) { 091 if (applicabilities.length == 0) { 092 return true; 093 } 094 for (Applicability a: applicabilities) { 095 if (a == applicability) { 096 return true; 097 } 098 } 099 return false; 100 } 101 102}