Skip to content

Instantly share code, notes, and snippets.

@shaneturner
Created November 9, 2023 20:40
Show Gist options
  • Save shaneturner/8e95e738b08176f239eebb3a12db1e74 to your computer and use it in GitHub Desktop.
Save shaneturner/8e95e738b08176f239eebb3a12db1e74 to your computer and use it in GitHub Desktop.
Calculate common screen rations from a given user input dimension. Scripts in both Bash .sh and Python .py
#!/usr/bin/env python3
def calculate_dimension(base_dimension, ratio):
calculated_dimension = int(base_dimension * ratio)
return calculated_dimension
def main():
dimension_choice = input("Enter 'w' for width or 'h' for height: ")
input_dimension = int(input("Enter the base dimension (as an integer): "))
print(f"\n")
base_dimension = input_dimension
ratios_w = {'16:9': 9 / 16, '4:3': 3 / 4, '21:9': 9 / 21, '16:10': 10 / 16, '5:4': 4 / 5}
ratios_h = {'16:9': 16 / 9, '4:3': 4 / 3, '21:9': 21 / 9, '16:10': 16 / 10, '5:4': 5 / 4}
ratios = ratios_w if dimension_choice.lower() == 'w' else ratios_h
dimension_type = 'Width' if dimension_choice.lower() == 'w' else 'Height'
# Output the calculated ratios and results.
print(f"Screen Ratios for {dimension_type} {base_dimension}:")
for ratio, ratio_value in ratios.items():
calculated_dimension = calculate_dimension(base_dimension, ratio_value)
formatted_dimension = int(calculated_dimension) if calculated_dimension % 1 == 0 else calculated_dimension
if dimension_choice.lower() == 'w':
print(f" {ratio} - {base_dimension}:{formatted_dimension}")
elif dimension_choice.lower() == 'h':
print(f" {ratio} - {formatted_dimension}:{base_dimension}")
if __name__ == "__main__":
main()
#!/bin/bash
# Function to check if a command is available
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to install 'bc' if not found
install_bc() {
if command_exists apt-get; then
sudo apt-get update
sudo apt-get install -y bc
elif command_exists yum; then
sudo yum install -y bc
else
echo "Please install 'bc' manually and rerun the script."
exit 1
fi
echo ""
echo ""
}
# Check if 'bc' is available
if ! command_exists bc; then
read -p "'bc' is not installed. Do you want to install it? (y/n): " install_choice
if [ "$install_choice" == "y" ]; then
install_bc
else
echo "Please install 'bc' manually and rerun the script."
exit 1
fi
fi
calculate_dimension() {
base_dimension=$1
ratio=$2
calculated_dimension=$(echo "scale=0; $base_dimension * $ratio" | bc -l)
echo $calculated_dimension
}
main() {
read -p "Enter 'w' for width or 'h' for height: " dimension_choice
read -p "Enter the base dimension (as an integer): " input_dimension
echo ""
base_dimension=$input_dimension
declare -A ratios_w=( ["16:9"]="9/16" ["4:3"]="3/4" ["21:9"]="9/21" ["16:10"]="10/16" ["5:4"]="4/5" )
declare -A ratios_h=( ["16:9"]="16/9" ["4:3"]="4/3" ["21:9"]="21/9" ["16:10"]="16/10" ["5:4"]="5/4" )
ratios=()
if [ "$dimension_choice" == "w" ]; then
ratios=("${!ratios_w[@]}") # Corrected line to store keys instead of values
dimension_type="Width"
elif [ "$dimension_choice" == "h" ]; then
ratios=("${!ratios_h[@]}") # Corrected line to store keys instead of values
dimension_type="Height"
else
echo "Invalid choice. Exiting."
exit 1
fi
# Output the calculated ratios and results.
echo "Screen Ratios for $dimension_type $base_dimension:"
for ratio in "${ratios[@]}"; do
ratio_value=${ratios_w["$ratio"]} # Use ratios_w for width and ratios_h for height
calculated_dimension=$(calculate_dimension $base_dimension $ratio_value)
formatted_dimension=$(echo $calculated_dimension | sed 's/\.[0-9]*$//')
if [ "$dimension_choice" == "w" ]; then
echo " $ratio - $base_dimension:$formatted_dimension"
elif [ "$dimension_choice" == "h" ]; then
echo " $ratio - $formatted_dimension:$base_dimension"
fi
done
}
if [ "$BASH_SOURCE" == "$0" ]; then
main
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment