|
| 1 | +package com.lazydash.audio.visualizer.system.persistance; |
| 2 | + |
| 3 | +import com.lazydash.audio.visualizer.system.config.ColorConfig; |
| 4 | +import javafx.scene.paint.Color; |
| 5 | + |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.FileWriter; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.util.Properties; |
| 13 | + |
| 14 | +public class ColorConfigPersistence { |
| 15 | + private String colorPropertiesLocation = "color.properties"; |
| 16 | + |
| 17 | + public static ColorConfigPersistence createAndReadColorConfiguration() { |
| 18 | + ColorConfigPersistence colorConfigPersistence = new ColorConfigPersistence(); |
| 19 | + colorConfigPersistence.readConfig(); |
| 20 | + return colorConfigPersistence; |
| 21 | + } |
| 22 | + |
| 23 | + private void readConfig() { |
| 24 | + Path colorPropertiesPath = Paths.get(colorPropertiesLocation); |
| 25 | + if (Files.exists(colorPropertiesPath)) { |
| 26 | + |
| 27 | + Properties appProps = new Properties(); |
| 28 | + try { |
| 29 | + appProps.load(new FileReader(colorPropertiesLocation)); |
| 30 | + |
| 31 | + String startColor = appProps.getProperty("startColor"); |
| 32 | + String endColor = appProps.getProperty("endColor"); |
| 33 | + |
| 34 | + ColorConfig.colorBands.get(0).setStartColor( |
| 35 | + Color.web(startColor) |
| 36 | + ); |
| 37 | + |
| 38 | + ColorConfig.colorBands.get(0).setEndColor( |
| 39 | + Color.web(endColor) |
| 40 | + ); |
| 41 | + |
| 42 | + } catch (IOException e) { |
| 43 | + e.printStackTrace(); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void persistConfig() { |
| 51 | + Properties appProps = new Properties(); |
| 52 | + |
| 53 | + Color startColor = ColorConfig.colorBands.get(0).getStartColor(); |
| 54 | + Color endColor = ColorConfig.colorBands.get(0).getEndColor(); |
| 55 | + |
| 56 | + String startColorHex = toRGBCode(startColor); |
| 57 | + String endColorHex = toRGBCode(endColor); |
| 58 | + |
| 59 | + appProps.setProperty("startColor", startColorHex); |
| 60 | + appProps.setProperty("endColor", endColorHex); |
| 61 | + |
| 62 | + try { |
| 63 | + appProps.store(new FileWriter(colorPropertiesLocation), "visualizer - colors"); |
| 64 | + |
| 65 | + } catch (IOException e) { |
| 66 | + e.printStackTrace(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private String toRGBCode( Color color ) { |
| 71 | + return String.format( "#%02X%02X%02X", |
| 72 | + (int)( color.getRed() * 255 ), |
| 73 | + (int)( color.getGreen() * 255 ), |
| 74 | + (int)( color.getBlue() * 255 ) ); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments