Skip to content

Instantly share code, notes, and snippets.

@xZise
Created January 10, 2012 16:50
Show Gist options
  • Select an option

  • Save xZise/1589988 to your computer and use it in GitHub Desktop.

Select an option

Save xZise/1589988 to your computer and use it in GitHub Desktop.
Resolution to Format
// Another way to handle the format related stuff
public class Resolution {
public static final Resolution FULL_HD = new Resolution(1920, 1080, "Full HD");
public static final Resolution HALF_HD = new Resolution(1280, 720, "Half HD");
public static final Resolution PAL = new Resolution( 720, 576, "PAL"); // Maybe 702?
public static final Resolution PALplus = new Resolution(1024, 576, "PALplus");
private static final RESOLUTIONS[] resolutions = new Resolution[] { FULL_HD, HALF_HD, PAL, PALplus };
public static final DecimalFormat DEFAULT_FORMAT = new DecimalFormat("#0.##");
public final int resX;
public final int resY;
public final String description;
public final String format;
private static String generateDescription(final int resX, final int resY) {
for (Resolution r : RESOLUTIONS) {
if (r.resX == resX && r.resY == resY) {
return r.description;
}
}
return "";
}
public Resolution(final int resX, final int resY) {
this(resX, resY, generateDescription(resX, resY));
}
private Resolution(final int resX, final int resY, final String description) {
this.resX = resX;
this.resY = resY;
this.description = description;
final double universalFormat = this.resX / (double) this.resY;
// 16:9 == 1.(7)
if (universalFormat > 1.77 && universalFormat < 1.7777778) {
this.format = "16:9";
// 16:10 == 1.6
} else if (universalFormat > 1.599999 && universalFormat < 1.6000001) {
this.format = "16:10";
// 4:3 == 1.(3)
} else if (universalFormat > 1.33 && universalFormat < 1.3333334) {
this.format = "4:3";
} else {
this.format = DEFAULT_FORMAT.format(universalFormat) + ":1";
}
}
public String getFormat() {
return this.format;
}
public String toString() {
final String minimal = this.resX + "x" + this.resY;
if (this.description != null && !this.description.isEmpty()) {
return minimal + " (" + this.description + ")";
}
return minimal;
}
}
public static final DecimalFormat DEFAULT_FORMAT = new DecimalFormat("#0.##");
public static String getFormatFromResolution(int resX, int resY) {
final double universalFormat = resX / (double) resY;
// 16:9 == 1.(7)
if (universalFormat > 1.77 && universalFormat < 1.7777778) {
return "16:9";
// 16:10 == 1.6
} else if (universalFormat > 1.599999 && universalFormat < 1.6000001) {
return "16:10";
// 4:3 == 1.(3)
} else if (universalFormat > 1.33 && universalFormat < 1.3333334) {
return "4:3";
} else {
return DEFAULT_FORMAT.format(universalFormat) + ":1";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment