I need the formulas to calculate any dpi to any dpi. For example: mdpi to hdpi, xdpi to xxhdpi, etc. This website exactly does what I require but I am not able to find the logic for it. Any suggestions on what will be the formula for it?
All the logic is client-side, so simple inspection will show You the whole algoritm:
I've extracted it and simplified a little bit:
if (e == ldpi) {
mdpi.value = (Number(ldpi.value) / 3) + Number(ldpi.value);
hdpi.value = Number(ldpi.value) * 2;
xhdpi.value = Number(mdpi.value) * 2;
xxhdpi.value = Number(mdpi.value) * 3;
xxxhdpi.value = Number(mdpi.value) * 4;
}
else if (e == mdpi) {
ldpi.value = (Number(mdpi.value) / 2) + (Number(mdpi.value) / 4);
hdpi.value = Number(ldpi.value) * 2;
xhdpi.value = Number(mdpi.value) * 2;
xxhdpi.value = Number(mdpi.value) * 3;
xxxhdpi.value = Number(mdpi.value) * 4;
}
else if (e == hdpi) {
ldpi.value = Number(hdpi.value) / 2;
mdpi.value = (Number(ldpi.value) / 3) + Number(ldpi.value);
xhdpi.value = Number(mdpi.value) * 2;
xxhdpi.value = Number(mdpi.value) * 3;
xxxhdpi.value = Number(mdpi.value) * 4;
}
else if (e == xhdpi) {
ldpi.value = Number(e.value) - (Number(e.value) / 2 + Number(e.value) / 8);
mdpi.value = Number(e.value) / 2;
hdpi.value = Number(ldpi.value) * 2;
xxhdpi.value = Number(mdpi.value) * 3;
xxxhdpi.value = Number(mdpi.value) * 4;
}
else if (e == xxhdpi) {
ldpi.value = Number(e.value) / 4;
mdpi.value = (Number(ldpi.value) / 3) + Number(ldpi.value);
hdpi.value = Number(ldpi.value) * 2;
xhdpi.value = Number(mdpi.value * 2);
xxxhdpi.value = Number(mdpi.value) * 4;
}
else if (e == xxxhdpi) {
const val = e.value / 4;
ldpi.value = val - (val / 4);
mdpi.value = (Number(ldpi.value) / 3) + Number(ldpi.value);
hdpi.value = Number(ldpi.value) * 2;
xhdpi.value = Number(mdpi.value * 2);
xxhdpi.value = Number(mdpi.value) * 3;
}
I think that the above code is pretty self explanatory, but the value e
represents some dpi
value. Then You should check if that value was ldpi
, mdpi
or any other and based on that You can calculate other densities.