本文目录导读:

获取显示颜色深度(Color Depth)的脚本方法取决于你使用的操作系统,以下是Windows、macOS和Linux的几种常见脚本实现方式:
Windows
PowerShell 脚本
# 获取主显示器的颜色深度
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class DisplayInfo {
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
public const int BITSPIXEL = 12;
public const int PLANES = 14;
}
"@
$hdc = [DisplayInfo]::GetDC([IntPtr]::Zero)
$bitsPerPixel = [DisplayInfo]::GetDeviceCaps($hdc, [DisplayInfo]::BITSPIXEL)
$planes = [DisplayInfo]::GetDeviceCaps($hdc, [DisplayInfo]::PLANES)
[DisplayInfo]::GetDC([IntPtr]::Zero) # 释放DC
Write-Host "颜色深度: $($bitsPerPixel * $planes) 位"
VBScript 脚本
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_VideoController")
For Each objItem In colItems
WScript.Echo "当前颜色深度: " & objItem.CurrentBitsPerPixel & " 位"
WScript.Echo "最大颜色深度: " & objItem.MaxMemorySupported & " 位"
Next
macOS
Bash/Shell 脚本
# 获取主显示器的颜色深度
system_profiler SPDisplaysDataType | grep "Depth" | awk '{print "颜色深度: "$NF}'
# 或者使用更详细的方法
I=0
system_profiler SPDisplaysDataType | while read line; do
if [[ $line == *"Color Depth"* ]]; then
echo "显示器 $I: $line"
((I++))
fi
done
Linux
使用 xdpyinfo (X11)
#!/bin/bash
# 获取默认显示器的颜色深度
xdpyinfo | grep "depth" | head -1 | awk '{print "颜色深度: "$2 " 位"}'
使用 xrandr (X11)
#!/bin/bash
# 获取当前显示器的颜色深度
xrandr --verbose | grep "Depth" | head -1 | awk '{print "颜色深度: "$2 " 位"}'
使用 /sys 文件系统
#!/bin/bash
# 获取第一个显示器的颜色深度
for card in /sys/class/drm/card*-*; do
if [ -f "$card/dpms" ]; then
depth=$(cat "$card/bits_per_channel" 2>/dev/null)
echo "$(basename $card): $depth bpc"
fi
done
Python 跨平台脚本
#!/usr/bin/env python3
import sys
if sys.platform == 'darwin': # macOS
import subprocess
result = subprocess.run(['system_profiler', 'SPDisplaysDataType'],
capture_output=True, text=True)
for line in result.stdout.split('\n'):
if 'Color Depth' in line:
print(f"颜色深度: {line.split(':')[1].strip()}")
elif sys.platform == 'linux': # Linux
import subprocess
result = subprocess.run(['xdpyinfo'], capture_output=True, text=True)
for line in result.stdout.split('\n'):
if 'depth' in line:
print(f"颜色深度: {line.split()[1]} 位")
elif sys.platform == 'win32': # Windows
import ctypes
user32 = ctypes.windll.user32
gdi32 = ctypes.windll.gdi32
hdc = user32.GetDC(0)
bits = gdi32.GetDeviceCaps(hdc, 12) # BITSPIXEL
planes = gdi32.GetDeviceCaps(hdc, 14) # PLANES
user32.ReleaseDC(0, hdc)
print(f"颜色深度: {bits * planes} 位")
使用说明
- Windows PowerShell: 以管理员身份运行可能会显示更全面的信息
- macOS:
system_profiler命令可能需要约2-3秒执行时间 - Linux:
xdpyinfo需要 X11 环境;Wayland 环境下可能需要使用wl-info或类似工具
选择适合你操作系统和使用场景的方法即可,建议使用系统自带的命令或工具,减少依赖。